它是UNION,AND,还是?

时间:2013-04-27 16:29:10

标签: mysql 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)

这有效,

我也有

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。会有其他东西吗?

感谢

1 个答案:

答案 0 :(得分: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)