sql查询中的多个条件

时间:2009-11-16 19:41:38

标签: php sql mysql

我想提取日期与今天相似的记录。

我有这个查询

$query = "select * from calendar_items WHERE expiredate LIKE '$todays_date %' 
    or upcoming_event_featured_date like '$todays_date %' 
    ORDER BY expiredate ASC";

现在我还有3个coming_event_featured_date:coming_event_featured_date2,coming_event_featured_date3,coming_event_featured_date4

所以我想运行一个查询,它将获取的结果的日期与四个coming_event_featured_date的日期相同(coming_event_featured_date,coming_event_featured_date2,coming_event_featured_date3,coming_event_featured_date4)。

查询会是这样的吗?

$query = "select * from calendar_items 
    WHERE expiredate LIKE '$todays_date %' or 
    (upcoming_event_featured_date like '$todays_date %' or 
    upcoming_event_featured_date2 like '$todays_date %' or 
    upcoming_event_featured_date3 like '$todays_date %' or 
    upcoming_event_featured_date4 like '$todays_date %') 
    ORDER BY expiredate ASC";

1 个答案:

答案 0 :(得分:1)

你一定要听HLGEM并规范化表格结构。但是对于你要求的内容,假设$ todays_date是一个实际日期(如2009-11-16),该查询可能会有效。但是,您可能需要将%移近,以便更安全一点:

$query = "select * from calendar_items 
WHERE expiredate LIKE '{$todays_date}%' or 
(upcoming_event_featured_date like '{$todays_date}%' or 
upcoming_event_featured_date2 like '{$todays_date}%' or 
upcoming_event_featured_date3 like '{$todays_date}%' or 
upcoming_event_featured_date4 like '{$todays_date}%') 
ORDER BY expiredate ASC";