我正在尝试创建一个查询,该查询返回属于start_date&内的club_id的所有行的商品。 end_date,但是查询还应返回与club_id匹配且end_date为0的结果 - 任何关于如何执行此操作的想法? 我目前的查询是......
SELECT
* ,
UNIX_TIMESTAMP( start_date ) AS start_dateStamp,
UNIX_TIMESTAMP( end_date ) AS end_dateStamp
FROM
(`offers`)
WHERE
UNIX_TIMESTAMP( `start_date` ) <1329308797
AND
UNIX_TIMESTAMP( `end_date` ) >1329308797
AND
`club_id` =23
答案 0 :(得分:2)
SELECT
`offers`.* ,
UNIX_TIMESTAMP( start_date ) AS start_dateStamp,
UNIX_TIMESTAMP( end_date ) AS end_dateStamp
FROM `offers`
WHERE `club_id` =23
AND (
(
`start_date`<FROM_UNIXTIME(1329308797)
AND `end_date`>FROM_UNIXTIME(1329308797)
)
OR `end_date`=FROM_UNIXTIME(0)
)
请注意,我将转换从unix-timestamp移动到MySQL-date从字段到常量 - 这样它只需转换一次,而不是所有行。另外,这样可以使用索引。
修改强>
“date zero”不是Unix-Zero而MySQL-Zeor最后一行应该是
OR `end_date`='0000-00-00'
此外,如果start_date
和end_date
的数据类型不是DATETIME
,而是DATE
则需要
DATE(FROM_UNIXTIME(...))
而不是
FROM_UNIXTIME(...)
答案 1 :(得分:1)
您没有指定end_date = 0
是否还需要start_date > $timestamp
,所以我认为start_date仍然必须符合其标准。
SELECT
please_name,
the_columns,
you_want_to_select_seperately,
for_reasons,
UNIX_TIMESTAMP(start_date) AS start_dateStamp,
UNIX_TIMESTAMP(end_date) AS end_dateStamp
FROM `offers`
WHERE `club_id` = 23
AND `start_date`) < UNIX_TIMESTAMP(1329308797)
AND (`end_date` > UNIX_TIMESTAMP(1329308797) OR `end_date` = "0000-00-00")
关于SQL的一些注意事项:
*
。从你原来的问题来看,你真的没有col BETWEEN min_val and max_val
(包含边界)NULL
。你有一个日期(2012-02-15)或你没有(NULL)。这样您的查询就可以只检查OR end_date IS NULL
答案 2 :(得分:0)
这是你想要的吗?
SELECT *
, UNIX_TIMESTAMP(start_date) AS start_dateStamp
, UNIX_TIMESTAMP(end_date) AS end_dateStamp
FROM
`offers`
WHERE
(UNIX_TIMESTAMP(`start_date`) < 1329308797 AND UNIX_TIMESTAMP(`end_date`) > 1329308797)
OR
(`club_id` = 23 AND `end_date` = '0000-00-00 00:00:00')