MySql选择Not Work

时间:2014-07-06 10:06:03

标签: mysql sql

我有两张桌子。

一张餐桌

id   type

1    lunch
2    lunch
3    dinner

两个user_history表

ID Meal_id  User_id  create  

1   2         4       1404638939  

现在我想从表一中选择所有餐点但有条件

如果表格two_id与膳食表格ID匹配并且创建与当前日期相同的日期,则跳过表格中的该行

我使用此代码但无法正常工作

SELECT m.* FROM `meal` AS m LEFT JOIN user_history
                 ON user_history.meal_id != m.id and date(FROM_UNIXTIME(user_history.create))!=CURRENT_DATE() where m.meal_type = 'Lunch'

2 个答案:

答案 0 :(得分:0)

SELECT m.* FROM `meal` AS m 
LEFT JOIN user_history
ON user_history.Meal_id != m.id and date(FROM_UNIXTIME(user_history.create))=CURRENT_DATE() 
where m.type LIKE "%lunch%"

答案 1 :(得分:0)

如果我正确理解了这个问题,我会说:

SELECT m.* FROM `meal` AS m 
    WHERE m.meal_type = 'Lunch'
        AND m.id NOT IN (
            SELECT meal_id FROM user_history
                WHERE date(FROM_UNIXTIME(user_history.create))=CURRENT_DATE()
        )