php mysql查询选择

时间:2016-10-27 23:55:34

标签: mysql

我有3张这样的表

news_table:
newsID int auto_increment,
title varchar(256),

记录如下: (1,'some_title'),

(2, 'some_title' ),
(3, 'some_title' ),
(4, 'some_title' ),
(5, 'some_title' ),
(6, 'some_title' ),
(7, 'some_title' ),
and so on...

news_exclude_table:

id int auto_increment
newsID int

记录如下:

('', 2),
('', 3),
('', 7),
('', 10)
and so on...

我删除了auto_increment值。该号码是相应的newsID

news_like_table:

id int auto_increment,
newsID int
memberID int

记录如下:

('', '2', '110'),
('', '4', '110'),
('', '4', '115'),
and so on

我想从news_table,news_like_table中选择符合特定条件的所有记录,但我想排除那些也在news_exclude_table中的记录 所以查询的选择结果应该只有newsID“4”

我无法弄清楚在这种情况下与PHP一起使用的mysql查询

select news_table.* from news_table, news_like_table, news_exclude_table where news_table.newsID = news_like_table.newsID and news_like_table.memberID='110' and news_table.newsID not in news_exclude_table.newsID

它应该选择newsID =“4”并省略newsID =“2”,因为“2”也在news_exclude_table中

我不能让这个工作。 请帮助。 感谢

1 个答案:

答案 0 :(得分:0)

切换到显式联接并将排除表保留在联接之外,并使用带有子查询的lodash,或者在另一个2上加入排除表,并在{中使用not in () {1}}条款。我会告诉你后者:

not null