为什么忽略MySQL语句?

时间:2014-01-05 12:07:06

标签: php mysql sql mysqli

为什么忽略n.author='$host[id]'

$host[id]=5;
SELECT  
n.id,n.name,n.text,
r.title,
COUNT(c.news_id) comments
FROM news n LEFT JOIN rub r
 ON
 r.news_id=n.id 
 LEFT JOIN comments c 
  ON 
n.id = c.news_id AND c.status='1' AND n.author='$host[id]'
GROUP BY n.id
 ORDER BY n.id DESC LIMIT 10

我只需要那些'news'。'author'= $ host [id]的行,但是执行这个查询我得到了表中的所有新闻。为什么会这样?

2 个答案:

答案 0 :(得分:0)

$host['id']=5;
SELECT  
n.id,n.name,n.text,
r.title,
COUNT(c.news_id) comments
FROM news n LEFT JOIN rub r
 ON
 r.news_id=n.id 
 LEFT JOIN comments c 
  ON 
n.id = c.news_id AND c.status='1' AND n.author="'{$host['id']}'";
GROUP BY n.id

答案 1 :(得分:0)

$host[id]=5;

SELECT n.id,n.name,n.text, r.title, COUNT(c.news_id) comments
FROM news n 
LEFT JOIN rub r ON r.news_id = n.id 
LEFT JOIN comments c ON c.news_id = n.id AND c.status = '1'
WHERE n.author = '$host[id]'
GROUP BY n.id
ORDER BY n.id DESC LIMIT 10