1

如何使用 MySQL 将这些组合成一个语句?

SELECT COUNT(barcode) AS count
FROM movieitemdetails_custom
WHERE username = 'John';

if count is == 0

    SELECT title FROM movieitemdetails WHERE barcode = '12345';

else

    SELECT title FROM movieitemdetails_custom WHERE username = 'John';

end-if
4

3 回答 3

1

这是获取列表的一种方法:

SELECT title
FROM movieitemdetails
WHERE barcode = '12345'
AND (
  SELECT COUNT(barcodes)
  FROM movieitemdetails_custom
  WHERE username = 'John'
) = 0
UNION SELECT title
FROM movieitemdetails_custom
WHERE username = 'John'
AND (
  SELECT COUNT(barcodes)
  FROM movieitemdetails_custom
  WHERE username = 'John'
) > 0

漫长的一天结束了,所以我希望我的计数为零/非零逻辑是直的:)

顺便说一句,我同意@Max 的观点,即不应该投反对票。我对这个问题投了赞成票。

于 2013-10-11T21:54:38.147 回答
0
 SELECT CASE WHEN countt = 0 THEN (SELECT barcodes FROM movieitemdetails WHERE username = 'John') 
 ELSE (SELECT barcodes FROM movieitemdetails_custom WHERE username = 'John')
于 2013-10-11T21:44:20.693 回答
0

一种选择是:

SELECT COALESCE(`mdc`.`title`, `md`.`title`, 'NO TITLE') `title`
FROM (SELECT NULL) der
    LEFT JOIN `movieitemdetails_custom` `mdc` ON `mdc`.`username` = 'John'
    LEFT JOIN `movieitemdetails` `md` ON `md`.`barcodes` = '1234567890';
于 2013-10-11T21:52:04.617 回答