MySQL难以选择

时间:2015-03-10 13:09:52

标签: mysql

对不起,我说一点英语。没关系:

SELECT `post`.`people`, `link`.`var`, 
  `post`.`date`, `private__tz`.`code` 
FROM `link` 
INNER JOIN `post` ON 
  `link`.`post_id` = `post`.`id` 
INNER JOIN `private__tz` ON `post`.`date_tz` = `private__tz`.`id` 
WHERE `link`.`category_id` = '" . $category [ 'id' ] . "' 
AND `link`.`state` IS NULL 
GROUP BY `link`.`var` 
ORDER BY `post`.`date` DESC

我想要me,但这不起作用:

SELECT IF(COUNT(
    SELECT * FROM `action` 
      WHERE `action`.`var`=`link`.`var`
  ))>0, 'yes', 'no') AS `me`, ...

感谢。

1 个答案:

答案 0 :(得分:1)

如果我正确理解您的问题(并且本身有点困难),您想在现有查询中添加新列(me)吗?如果是,则需要outer join到表private__het__action。然后,您可以在。

中添加IF语句

也许是这样的:

SELECT `private__het__post`.`people`, `private__het__link`.`var`, `private__het__post`.`date`, `private__tz`.`code`,
   if (COUNT(`private__het__action`.`var`) > 0, 'yes', 'no') `me`
FROM `private__het__link` 
   INNER JOIN `private__het__post` ON `private__het__link`.`post_id` = `private__het__post`.`id` 
   INNER JOIN `private__tz` ON `private__het__post`.`date_tz` = `private__tz`.`id` 
   LEFT JOIN `private__het__action` ON `private__het__action`.`var`=`private__het__link`.`var`
WHERE ...
顺便说一下,如果你使用表别名,你的生活会更容易。