AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 149709
Accepted
StixO
StixO
Asked: 2016-09-15 11:11:04 +0800 CST2016-09-15 11:11:04 +0800 CST 2016-09-15 11:11:04 +0800 CST

MySQL 行到 Columns.Pivot 表

  • 772

我正在尝试将结果集转换为列集 - MySQL 中的行到列。我只从多行中返回一个值以根据行放入列中 - 仅作为 1 条记录 - 并且列标题仅对应于返回的行号。

这是我的表格和代码。我有两个表,一个主表和一个用于多对多关系的中间表。Primary Table = locations - Columns Id, SeqNumber - SeqNumber 匹配另一个系统 1 for 1. Intermediary Table = boxlocations Columns locationId, otherTableID, IsSelected, otherProperty

我想得到的结果是:

应该恰好是 1 行结果,其中列代表返回的每一行。列中的数据应遵循此(如果 IsSelected=1 对于结果中的给定行,则该列应具有 SeqNumber Else -1 )我可能会提到此表位置只有 14 行,我只需要此 SeqNumber在查询的列中 - 并且列需要匹配从位置表的查询返回的行数。

SELECT      `locations`.`SeqNumber`, BL.IsSelected , 
            SUM(CASE IsSelected WHEN 1 THEN SeqNumber ELSE -1 END) Location_Id_1,          
            SUM(CASE IsSelected WHEN 1 THEN SeqNumber ELSE -1 END) Location_Id_2,
            SUM(CASE IsSelected WHEN 1 THEN SeqNumber ELSE -1 END) Location_Id_3,
            SUM(CASE IsSelected WHEN 1 THEN SeqNumber ELSE -1 END) Location_Id_4,
            SUM(CASE IsSelected WHEN 1 THEN SeqNumber ELSE -1 END) Location_Id_5,
            SUM(CASE IsSelected WHEN 1 THEN SeqNumber ELSE -1 END) Location_Id_6,
            SUM(CASE IsSelected WHEN 1 THEN SeqNumber ELSE -1 END) Location_Id_7,
            SUM(CASE IsSelected WHEN 1 THEN SeqNumber ELSE -1 END) Location_Id_8,
            SUM(CASE IsSelected WHEN 1 THEN SeqNumber ELSE -1 END) Location_Id_9,
            SUM(CASE IsSelected WHEN 1 THEN SeqNumber ELSE -1 END) Location_Id_10,
            SUM(CASE IsSelected WHEN 1 THEN SeqNumber ELSE -1 END) Location_Id_11,
            SUM(CASE IsSelected WHEN 1 THEN SeqNumber ELSE -1 END) Location_Id_12,
            SUM(CASE IsSelected WHEN 1 THEN SeqNumber ELSE -1 END) Location_Id_13,
            SUM(CASE IsSelected WHEN 1 THEN SeqNumber ELSE -1 END) Location_Id_14  

FROM locations LEFT OUTER JOIN 
(SELECT * FROM boxlocations WHERE boxlocations.BoxID = boxid) As BL
 ON BL.locationID = locations.Id ORDER BY SeqNumber; 

我也尝试使用 MAX If ,MAX(IF(IsSelected = 1, Ndx, -1)) 我尝试使用这个 SELECT locations.SeqNumber, BL.IsSelected , ( @curRank := @curRank + 1 ) 作为等级,但是我得到未知的领域排名。

有很多关于 SO 的文章——我从那里得到了两个例子和排名

有没有人有解决这个问题的简单方法?- 我愿意添加存储过程、通用过程等 - 但解决方案必须全部是纯 MySQL 代码。

/* EDIT 9-14-2016:3:20 */ 每个请求:这是两个表模式和数据的转储。

CREATE DATABASE  IF NOT EXISTS `DevDataBase` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `DevDataBase`;
-- MySQL dump 10.13  Distrib 5.7.9, for Win64 (x86_64)
--
-- Host: localhost    Database: DevDataBase
-- ------------------------------------------------------
-- Server version   5.7.12-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `boxlocations`
--

DROP TABLE IF EXISTS `boxlocations`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `boxlocations` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `boxID` int(11) NOT NULL DEFAULT '0' COMMENT 'References the Box.ID column of Table Boxs',
  `locationID` int(11) NOT NULL DEFAULT '0' COMMENT 'References the Location.ID column of Table Locations',
  `recipeId` int(11) NOT NULL,
  `IsSelected` int(1) DEFAULT '0' COMMENT 'IS the location enabled for the selected box.',
  PRIMARY KEY (`ID`),
  UNIQUE KEY `ID_UNIQUE` (`ID`),
  KEY `FK_BOXID_BOXID_idx` (`boxID`),
  KEY `FK_LOCID_LOCID_idx` (`locationID`),
  CONSTRAINT `FK_BOXID_BOXID` FOREIGN KEY (`boxID`) REFERENCES `boxs` (`Id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `FK_LOCID_LOCID` FOREIGN KEY (`locationID`) REFERENCES `locations` (`Id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8 COMMENT='Intermediary Table [aka Associative Table] for boxs and leng';
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `boxlocations`
--

LOCK TABLES `boxlocations` WRITE;
/*!40000 ALTER TABLE `boxlocations` DISABLE KEYS */;
INSERT INTO `boxlocations` VALUES (1,1,1,0,1),(2,2,1,0,1),(3,2,2,0,1),(4,2,20,0,1),(5,4,20,0,1),(6,4,10,0,1),(7,6,10,0,1),(8,6,11,0,0),(9,6,3,0,1),(10,6,5,0,1),(11,6,7,0,1),(12,6,4,0,1),(13,6,1,0,1),(14,1,8,0,1),(15,1,10,0,1),(16,1,12,0,1);
/*!40000 ALTER TABLE `boxlocations` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `locations`
--

DROP TABLE IF EXISTS `locations`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `locations` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `Name` varchar(82) DEFAULT '""',
  `Min` decimal(11,3) DEFAULT '0.000',
  `Max` decimal(11,3) DEFAULT '0.000',
  `Nom` decimal(11,3) DEFAULT '0.000',
  `Actual_Real` decimal(11,3) DEFAULT '0.000',
  `Set_Default` int(11) DEFAULT '0',
  `Set_Enable` int(11) DEFAULT '0',
  `Visible` int(11) DEFAULT '0',
  `SeqNumber` int(11) DEFAULT NULL,
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `locations`
--

LOCK TABLES `locations` WRITE;
/*!40000 ALTER TABLE `locations` DISABLE KEYS */;
INSERT INTO `locations` VALUES (1,'LOCATION1',0.000,0.000,0.000,0.000,1,0,0,1),(2,'LOCATION2',0.100,0.200,0.000,0.000,0,1,0,2),(3,'LOCATION3',0.000,0.000,0.000,0.000,0,0,0,3),(4,'LOCATION4',0.000,0.000,0.000,0.000,0,0,0,4),(5,'LOCATION5',0.000,0.000,0.000,0.000,0,0,0,5),(6,'LOCATION6',0.000,0.000,0.000,0.000,0,0,0,6),(7,'LOCATION7',0.000,0.000,0.000,0.000,0,0,0,7),(8,'LOCATION8',0.000,0.000,0.000,0.000,0,0,0,8),(9,'LOCATION9',0.000,0.000,0.000,0.000,0,0,0,9),(10,'LOCATION10',0.000,0.000,0.000,0.000,0,0,0,10),(11,'LOCATION11',0.000,0.000,0.000,0.000,0,0,0,11),(12,'LOCATION12',0.000,0.000,0.000,0.000,0,0,0,12),(13,'LOCATION13',0.000,0.000,0.000,0.000,0,0,0,13),(14,'LOCATION14',0.000,0.000,0.000,0.000,0,0,0,14),(15,'LOCATION15',0.000,0.000,0.000,0.000,0,0,0,15),(16,'LOCATION16',0.000,0.000,0.000,0.000,0,0,0,16),(17,'LOCATION17',0.000,0.000,0.000,0.000,0,0,0,17),(18,'LOCATION18',0.000,0.000,0.000,0.000,0,0,0,18),(19,'LOCATION19',0.000,0.000,0.000,0.000,0,0,0,19),(20,'LOCATION20',0.000,0.000,0.000,0.000,0,0,0,20);
/*!40000 ALTER TABLE `locations` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2016-09-14 15:48:50

AND HERE 是不同尝试的两个结果。 在此处输入图像描述

在此处输入图像描述

mysql pivot
  • 1 1 个回答
  • 1110 Views

1 个回答

  • Voted
  1. Best Answer
    Rick James
    2016-09-15T17:29:33+08:002016-09-15T17:29:33+08:00

    也许这更接近你想要的?不是,看看它是否为您提供了有关如何“旋转”的线索。

    SELECT  
            SUM(if(ID = 1, locationID, 0)) Location_Id_1,          
            SUM(if(ID = 2, locationID, 0)) Location_Id_2,          
            SUM(if(ID = 3, locationID, 0)) Location_Id_3,          
            SUM(if(ID = 4, locationID, 0)) Location_Id_4,          
            SUM(if(ID = 5, locationID, 0)) Location_Id_5
        FROM boxlocations;
    

    (MAX()应该给出与 相同的结果SUM()。)

    • 1

相关问题

  • 是否有任何 MySQL 基准测试工具?[关闭]

  • 我在哪里可以找到mysql慢日志?

  • 如何优化大型数据库的 mysqldump?

  • 什么时候是使用 MariaDB 而不是 MySQL 的合适时机,为什么?

  • 组如何跟踪数据库架构更改?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve