我有一個獨特的情況。我有一個函數可以即時生成SQl並在mySQL數據庫上運行它。它通過構建SQL查詢步驟並且非常複雜。從第二個表格無JOIN訂購
它在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卡車在何處進行= '沃爾沃' 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
使用'join'正是你需要做的。你爲什麼試圖避免這種情況? –
您現有的動態SQL寫入函數可以處理連接嗎?如果不是,你可以重寫嗎? –
@Mark Bannister。我擔心我將不得不重寫應用程序。它的老,長和醜,並以一種稱爲雙螺旋的語言。所以我真的試圖避免它。它需要被廢棄,但我希望樂隊提供援助,直到我有足夠的預算將整件事情拋出:聘請兩個新人並從頭開始重寫。 – JVMX