我有一个独特的情况。我有一个函数可以动态生成SQl并在mySQL数据库上运行它。它通过ste构建SQL查询步骤,并且非常复杂。
WHERE子句中最多可以包含40个不同的AND。例如。
SELECT * FROM TableX //yea I know don't search for * ... trying to save typing on stack.
WHERE Size = 'Large'
AND color= 'blue'
AND smell = 'stinky'
AND ugly = 'no'
AND brand = 'United'
etc...
在结束时,它会输出一行ORDER BY。如:
ORDER BY brand
我的挑战是我只能使用ORDER BY 某些字符串进行订购。如果我想从主表中订购数据,这可以正常工作。但是,如果它出自相关表格,我该怎么办?
说我有以下架构:
CREATE TABLE `Trucks` (
`ID` INT NOT NULL AUTO_INCREMENT ,
`Make` VARCHAR(45) NULL ,
`Current_PartList_ID` INT NULL ,
PRIMARY KEY (`ID`) );
INSERT INTO `Trucks` (`Make`, `Current_PartList_ID`) VALUES ('Volvo', '1');
INSERT INTO `Trucks` (`Make`, `Current_PartList_ID`) VALUES ('Volvo', '2');
INSERT INTO `Trucks` (`Make`, `Current_PartList_ID`) VALUES ('Mac', '3');
INSERT INTO `Trucks` (`Make`, `Current_PartList_ID`) VALUES ('Mac', '5');
INSERT INTO `Trucks` (`Make`, `Current_PartList_ID`) VALUES ('Daihatsu', '8');
INSERT INTO `Trucks` (`Make`, `Current_PartList_ID`) VALUES ('Volvo', '4');
CREATE TABLE `Parts_lists` (
`ID` INT NOT NULL AUTO_INCREMENT ,
`Carb_Model` VARCHAR(45) NULL ,
`Carb_date` DATE NULL ,
`Tire_type` VARCHAR(45) NULL ,
`Tire_date` DATE NULL ,
PRIMARY KEY (`ID`) );
INSERT INTO `Parts_lists` (`Carb_Model`, `Carb_date`, `Tire_type`, `Tire_date`) VALUES ('Hirsch', '2012-12-19', 'Toyo', '2013-01-01');
INSERT INTO `Parts_lists` (`Carb_Model`, `Carb_date`, `Tire_type`, `Tire_date`) VALUES ('HIrsch', '2013-02-14', 'Goodyear', '2011-03-16');
INSERT INTO `Parts_lists` (`Carb_Model`, `Carb_date`, `Tire_type`, `Tire_date`) VALUES ('Bosch', '2011-11-04', 'Toyo', '2013-01-01');
INSERT INTO `Parts_lists` (`Carb_Model`, `Carb_date`, `Tire_type`, `Tire_date`) VALUES ('Miller', '2009-10-11', 'Toyo', '2010-04-17');
INSERT INTO `Parts_lists` (`Carb_Model`, `Carb_date`, `Tire_type`, `Tire_date`) VALUES ('Bosch', '2011-01-07', 'Goodyear', '2013-01-06');
INSERT INTO `Parts_lists` (`Carb_Model`, `Carb_date`, `Tire_type`, `Tire_date`) VALUES ('Bosch', '2012-09-16', 'Lamb', '2012-06-25');
INSERT INTO `Parts_lists` (`Carb_Model`, `Carb_date`, `Tire_type`, `Tire_date`) VALUES ('Miller', '2011-07-22', 'Unknown', '2012-04-07');
INSERT INTO `Parts_lists` (`Carb_Model`, `Carb_date`, `Tire_type`, `Tire_date`) VALUES ('Davis', '2009-03-09', 'Hawking', '2012-06-16');
INSERT INTO `Parts_lists` (`Carb_Model`, `Carb_date`, `Tire_type`, `Tire_date`) VALUES ('Sanno', '2010-01-07', 'Goodyear', '2009-07-16');
INSERT INTO `Parts_lists` (`Carb_Model`, `Carb_date`, `Tire_type`, `Tire_date`) VALUES ('Thrust', '2012-11-11', 'Lamb', '2004-04-08');
我想得到:
SELECT * FROM Trucks WHERE Make ='volvo'ORDER BY(* Parts_List.Carb_date *)
因此给我以下选择
ID Make (why)
6 Volvo (Because the Carb_date is 2009-10-11)
1 Volvo (Because the Carb_date is 2012-12-19)
2 Volvo (Because the Carb_date is 2013-02-14)
非常清楚:我坚持:以下文字(我根本无法编辑它:没有完全重写一个古老丑陋的应用程序):
SELECT * FROM Trucks WHERE Make = 'volvo' ORDER BY
我需要XXXXXXX的字符串
SELECT * FROM Trucks WHERE Make = 'volvo' ORDER BY XXXXXXX
答案 0 :(得分:2)
这可以作为ORDER BY
字符串:
(SELECT Carb_date FROM Parts_lists WHERE ID = Trucks.Current_PartList_ID)