1

我的查询有点问题。我需要选择任何可能的组合...我正在尝试通过查询在 db 中搜索

"SELECT * FROM `events` WHERE
                            `start_time` >= '{$current_time}' AND
                            `start_time` >=  '{$limit_from}' AND
                            `end_time` <= '{$limit_to}' AND
                            `filter_large` = '{$filter_large}'"

它给了我 3 个结果和第二个

"SELECT * FROM `events` WHERE
                            `start_time` >= '{$current_time}' AND
                            `start_time` >=  '{$limit_from}' AND
                            `end_time` <= '{$limit_to}' AND
                            `filter_education` = '{$filter_education}'"

给了我 4 个结果,但我想像这样一起查询它们..

"SELECT * FROM `events` WHERE
                          (
                            `start_time` >= '{$current_time}' AND
                            `start_time` >=  '{$limit_from}' AND
                            `end_time` <= '{$limit_to}' AND
                            `filter_education` = '{$filter_education}'
                          ) OR (
                            `start_time` >= '{$current_time}' AND
                            `start_time` >=  '{$limit_from}' AND
                            `end_time` <= '{$limit_to}' AND
                            `filter_large` = '{$filter_large}'
                          )

它只给我 4 个结果而不是 7 个。我做错了什么?

4

4 回答 4

3

尝试使用UNION运算符

SELECT * FROM `events` WHERE
(
    `start_time` >= '{$current_time}' AND
    `start_time` >=  '{$limit_from}' AND
    `end_time` <= '{$limit_to}' AND
    `filter_education` = '{$filter_education}'
)

UNION

SELECT * FROM `events` WHERE
(
    `start_time` >= '{$current_time}' AND
    `start_time` >=  '{$limit_from}' AND
    `end_time` <= '{$limit_to}' AND
    `filter_large` = '{$filter_large}'
)
于 2012-04-09T15:17:47.550 回答
2

给了我 3 个结果

第二个给了我 4 个结果

想同时查询他们两个

它只给我 4 个结果而不是 7 个

这可能是因为这两个查询共享三个记录。唯一的区别是您的最后一个条件,因此您可以将它们组合起来,而其他条件保持不变:

SELECT * 
FROM `events` 
WHERE `start_time` >= '{$current_time}' 
AND `start_time` >=  '{$limit_from}' 
AND `end_time` <= '{$limit_to}' 
AND (`filter_large` = '{$filter_large}' OR `filter_education` = '{$filter_education}')
于 2012-04-09T15:19:02.580 回答
0

UNION用于将多个 SELECT 语句的结果组合成一个结果集。

希望这有帮助:D

于 2012-04-09T15:20:34.193 回答
0

我想你想要一个 UNION:

"SELECT * FROM `events` WHERE
`start_time` >= '{$current_time}' AND
`start_time` >=  '{$limit_from}' AND
`end_time` <= '{$limit_to}' AND
`filter_large` = '{$filter_large}'
UNION
SELECT * FROM `events` WHERE
`start_time` >= '{$current_time}' AND
`start_time` >=  '{$limit_from}' AND
`end_time` <= '{$limit_to}' AND
`filter_education` = '{$filter_education}'"
于 2012-04-09T15:17:51.477 回答