0

给定以下数据库:

CREATE TABLE album ( id int );

INSERT INTO album (id) VALUES
(1),
(2),
(3),
(4);

CREATE TABLE icon_album ( albumID int, current int );

INSERT INTO icon_album (albumID, current) VALUES
(1, 1),
(1, 1),

(2, 1),
(2, 0),

(3, 0),
(3, 0);

我想得到以下结果

albums:  id   status
         1      1
         2      0
         3      0
         4      0

能给我正确结果的 MySql 查询是什么?

PS 1:这是我对这个问题的第二个问题。第一个问题没有产生有效的解决方案

4

2 回答 2

1

这是你要找的吗?

SELECT a.id, IF(i.current IS NULL, 0, current) AS status
FROM album a LEFT JOIN
(
    SELECT albumID, MIN(current) AS current
    FROM icon_album
    GROUP BY albumID
) i ON a.id = i.albumID
于 2013-11-13T10:47:04.393 回答
0

像这样试试

"SELECT albumID AS id, if(SUM(current)>1,1,0) AS status FROM icon_album GROUP BY albumID"
于 2013-11-13T10:30:48.603 回答