我正在尝试将结果集转换为列集 - 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
也许这更接近你想要的?不是,看看它是否为您提供了有关如何“旋转”的线索。
(
MAX()
应该给出与 相同的结果SUM()
。)