MySQL / Drupal:JOIN ON语句中的条件

时间:2015-03-19 13:01:16

标签: mysql drupal

我设法挂起我的数据库服务器进行查询:

SELECT d.name, COUNT(DISTINCT stats_log.id ) AS hits, COUNT( DISTINCT stats_log.ip ) AS views
FROM stats_log
LEFT JOIN node ON node.nid = stats_log.nid
LEFT JOIN  taxonomy_term_data d ON d.tid IN (node.category_tid, node.category_tid2)
GROUP BY d.tid
ORDER BY hits

但这很好用:

SELECT d.name, COUNT(DISTINCT stats_log.id ) AS hits, COUNT( DISTINCT stats_log.ip ) AS views
FROM stats_log
LEFT JOIN node ON node.nid = stats_log.nid
LEFT JOIN  taxonomy_term_data d ON node.category_tid = d.tid
GROUP BY d.tid
ORDER BY hits

所以我的问题是......我只是没有注意到我的查询中的一些错误,或者我有一些限制或有趣的行为,我不知道?

  • node.category_tid2值大多为NULL
  • d.tid是主键

1 个答案:

答案 0 :(得分:0)

这里有一个SQL问题

LEFT JOIN taxonomy_term_data d ON d.tid IN(node.category_tid,node.category_tid2)

应该是:

LEFT JOIN taxonomy_term_data d ON(d.tid = node.category_tid OR d.tid = node.category_tid2)