MySQL条件LEFT JOIN?

时间:2016-03-10 01:47:23

标签: mysql sql

我有三个表:RewriteEngine on #don't do anything if index.php is called directly RewriteRule ^index\.php - [L] #if other php file called(user typed it in), redirect to extensionless uri RewriteCond %{THE_REQUEST} ^GET\ /(.+)\.php RewriteRule ^ /%1? [R=302,L] #if extensionless uri maps to a real php file, forward to index.php RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [OR] #or if not a real file or directory forward to index.php RewriteCond %{SCRIPT_FILENAME} !-d RewriteCond %{SCRIPT_FILENAME} !-f RewriteRule ^(.+)/?$ /index.php [L,NC] OnInit: function(){ ... this.oPlanningCalendar = this.byId("PC1") var oBinding = oPlanningCalendar.getBinding("rows"); oBinding.attachDataReceived(this.fnDataReceived, this); OnInit: function(){ ... this.oPlanningCalendar = this.byId("PC1"); this.oPlanningCalendar.updateRows = function(sReason) { this.oPlanningCalendar.updateAggregation("rows"); var oBinding = this.oPlanningCalendar.getBinding("rows"); if (oBinding) { jQuery.sap.log.info("max rows = " + oBinding.getLength() || 0); } }.bind(this); 。 表格如下所示:

albums表:

songs

images表:

albums

id | title | image_id 5 | First Album | 1 6 | Another Album | 2 表:

songs

我正在尝试显示带有标题和相应图像的歌曲列表。

如果歌曲没有图像(如果id | album_id | title | image_id 32 | 5 | My Song | 3 33 | 5 | Another One | 4 34 | 5 | First Song | 0 35 | 6 | My Song #2 | 0 36 | 6 | Fancy Title | 0 37 | 6 | My Love Song | 5 列为images),那么我只想使用相册图片。

到目前为止我唯一的代码就是这个,我被困住了:

id   |     path
1    | path/to/image1.jpg
2    | path/to/image2.jpg
3    | path/to/image3.jpg
4    | path/to/image4.jpg
5    | path/to/image5.jpg

如果它是一个可以处理大型表的高效查询,那就太好了。

3 个答案:

答案 0 :(得分:3)

这样的事情应该有效:

SELECT s.title, COALESCE(si.path, ai.path)
FROM albums AS a
INNER JOIN songs s ON a.id = s.album_id
LEFT JOIN images ai ON i.id = a.image_id
LEFT JOIN images si ON i.id = s.image_id

请注意,您必须将images表加入albums表和songs表。 aisi充当这些联接的单独命名空间,以便COALESCE知道如何查找可供选择的选项。

(请注意INNER JOIN是值得商榷的,具体取决于您对数据的期望。)

答案 1 :(得分:1)

您也可以这样使用UNION:

SELECT songs.title, images.path
FROM songs
LEFT JOIN images ON (images.id = songs.image_id)
WHERE songs.image_id != 0
UNION
SELECT songs.title, images.path
FROM songs
LEFT JOIN albums ON (songs.album_id = albums.id)
LEFT JOIN images ON (images.id = albums.image_id)
WHERE albums.image_id != 0 AND songs.image_id = 0

基本上,您首先选择带有图像的所有歌曲,然后如果歌曲本身没有,则将所有歌曲附加到专辑图像。

答案 2 :(得分:1)

这是一个使用多个LEFT JOIN的解决方案,使用您提供的数据进行演示。

最终查询:

SELECT 
    s.title, 
    COALESCE(i1.path, i2.path)
FROM songs s
LEFT JOIN albums a ON s.album_id = a.id
LEFT JOIN images i1 ON s.image_id = i1.id
LEFT JOIN images i2 ON a.image_id = i2.id
ORDER BY s.id;

以下是完整演示。

SQL:

-- Data for demo
create table albums(id int, title char(100), image_id int);
insert into albums values
(5    ,  'First Album'  ,      1),
(6    , 'Another Album' ,      2);

create table songs(id int, album_id int, title char(100), image_id int);
insert into songs values
(32    ,      5       ,    'My Song'    ,      3),
(33    ,      5       ,  'Another One'  ,      4),
(34    ,      5       ,   'First Song'  ,      0),
(35    ,      6       ,   'My Song #2'  ,      0),
(36    ,      6       ,  'Fancy Title'  ,      0),
(37    ,      6       ,  'My Love Song' ,      5);

create table images(id int, path char(200));
insert into images values
(1    , 'path/to/image1.jpg'),
(2    , 'path/to/image2.jpg'),
(3    , 'path/to/image3.jpg'),
(4    , 'path/to/image4.jpg'),
(5    , 'path/to/image5.jpg');

SELECT * FROM albums;
SELECT * FROM songs;
SELECT * FROM images;

-- SQL needed 
SELECT 
    s.title, 
    COALESCE(i1.path, i2.path)
FROM songs s
LEFT JOIN albums a ON s.album_id = a.id
LEFT JOIN images i1 ON s.image_id = i1.id
LEFT JOIN images i2 ON a.image_id = i2.id
ORDER BY s.id;

输出:

mysql> SELECT
    -> s.title,
    -> COALESCE(i1.path, i2.path)
    -> FROM songs s
    -> LEFT JOIN albums a ON s.album_id = a.id
    -> LEFT JOIN images i1 ON s.image_id = i1.id
    -> LEFT JOIN images i2 ON a.image_id = i2.id
    -> ORDER BY s.id;
+--------------+----------------------------+
| title        | COALESCE(i1.path, i2.path) |
+--------------+----------------------------+
| My Song      | path/to/image3.jpg         |
| Another One  | path/to/image4.jpg         |
| First Song   | path/to/image1.jpg         |
| My Song #2   | path/to/image2.jpg         |
| Fancy Title  | path/to/image2.jpg         |
| My Love Song | path/to/image5.jpg         |
+--------------+----------------------------+
6 rows in set (0.00 sec)