2013-04-27 91 views
2

我已經嘗試了所有,有沒有其他的語法,將工作?我有以下它是聯盟,還是或?

SELECT `ID` , `Home_Team` 
FROM `FixturesDates` where `ID` = 
    (SELECT `ID` 
    FROM `FixturesDates` 
    WHERE `Home_Team`  
     REGEXP 'saturday|sunday|monday|tuesday|wednesday|thursday|friday' 
    order by ID asc LIMIT 1) 

這個作品,

我也有

SELECT `ID` , `Home_Team` 
FROM `FixturesDates` 
where `ID` = 
    (SELECT `ID` FROM `FixturesDates` WHERE `Home_Team` 
    REGEXP 'saturday|sunday|monday|tuesday|wednesday|thursday|friday' 
    order by ID asc LIMIT 1,1) 

這也適用,但即時通訊無法將2相結合,試圖

SELECT `ID` , `Home_Team` 
FROM `FixturesDates` 
where `ID` = 
    (SELECT `ID` FROM `FixturesDates` WHERE `Home_Team` 
    REGEXP 'saturday|sunday|monday|tuesday|wednesday|thursday|friday' 
    order by ID asc LIMIT 1) 
UNION 
    (SELECT `ID` FROM `FixturesDates` WHERE `Home_Team` 
    REGEXP 'saturday|sunday|monday|tuesday|wednesday|thursday|friday' 
    order by ID asc LIMIT 1,1) 

但這不起作用,AND和OR也不起作用。有什麼其他的嗎?

謝謝

回答

0

你不能合併它們,因爲它們有不同的列。

嘗試增加NULLHome_Team到第二子查詢:

SELECT `ID` , `Home_Team` 
FROM `FixturesDates` 
where `ID` = 
    (SELECT `ID` FROM `FixturesDates` WHERE `Home_Team` 
    REGEXP 'saturday|sunday|monday|tuesday|wednesday|thursday|friday' 
    order by ID asc LIMIT 1) 
UNION 
    (SELECT `ID`, NULL as Home_Team FROM `FixturesDates` WHERE `Home_Team` 
    REGEXP 'saturday|sunday|monday|tuesday|wednesday|thursday|friday' 
    order by ID asc LIMIT 1,1) 

或者更簡單地說:

SELECT `ID` , `Home_Team` 
FROM `FixturesDates` 
where `ID` in 
    (SELECT `ID` FROM `FixturesDates` WHERE `Home_Team` 
    REGEXP 'saturday|sunday|monday|tuesday|wednesday|thursday|friday' 
    order by ID asc LIMIT 2) 

這是基於相同的查詢。也許你的意思是這樣:

SELECT `ID` , `Home_Team` 
FROM `FixturesDates` 
where `ID` = 
    (SELECT `ID` FROM `FixturesDates` WHERE `Home_Team` 
    REGEXP 'saturday|sunday|monday|tuesday|wednesday|thursday|friday' 
    order by ID asc LIMIT 1) 
    OR ID = 
    (SELECT `ID`, NULL as Home_Team FROM `FixturesDates` WHERE `Home_Team` 
    REGEXP 'saturday|sunday|monday|tuesday|wednesday|thursday|friday' 
    order by ID asc LIMIT 1,1) 

你的兩個原始查詢的正確聯盟將包括查詢:

SELECT `ID` , `Home_Team` 
FROM `FixturesDates` where `ID` = 
    (SELECT `ID` 
    FROM `FixturesDates` 
    WHERE `Home_Team`  
     REGEXP 'saturday|sunday|monday|tuesday|wednesday|thursday|friday' 
    order by ID asc LIMIT 1) 
union 
SELECT `ID` , `Home_Team` 
FROM `FixturesDates` 
where `ID` = 
    (SELECT `ID` FROM `FixturesDates` WHERE `Home_Team` 
    REGEXP 'saturday|sunday|monday|tuesday|wednesday|thursday|friday' 
    order by ID asc LIMIT 1,1) 
+0

第一個應該工作作爲列不爲空,可以看到單獨運行查詢的第二部分,所以我不知道它爲什麼說NULL,但是你發佈的最後一個查詢是完美的。謝謝 – Mike 2013-04-27 17:07:05

+0

@Mike。 。 。我現在意識到你打算在'where'子句中做'union'。爲此,您需要一組額外的parens:'((select ...)union(select ...))'中的id。 'union'適用於整個查詢。 – 2013-04-27 17:19:13

+0

完美,謝謝。 – Mike 2013-04-27 17:47:10