从mysql中的多个表中获取多个值?

时间:2014-06-03 03:57:05

标签: php mysql yii

我只是在网上制作音乐网站。但是当我试图获得按用户顺序创建的所有曲目,播放列表,专辑时,我陷入了困境。我有4个表,包括:用户,曲目,专辑,播放列表,如下所示:

+---------------+------------------+
| Field         | Type             | 
+---------------+------------------+
| album_id      | int(10) unsigned |
| user_id       | int(10) unsigned |
| album_title   | varchar(255)     |
| album_created | datetime()       |

+---------------+------------------+
| Field         | Type             | 
+---------------+------------------+
| playlist_id   | int(10) unsigned |
| user_id       | int(10) unsigned |
| playlist_title| varchar(255)     |
| plist_created | datetime()       |

+---------------+------------------+
| Field         | Type             | 
+---------------+------------------+
| track_id      | int(10) unsigned |
| user_id       | int(10) unsigned |
| track_title   | varchar(255)     |
| track_created | datetime()       |

+---------------+------------------+
| Field         | Type             | 
+---------------+------------------+
| user_id      | int(10) unsigned |
| user_name    | varchar(255      |
| password     | varchar(255)     |
| email        | varchar(255      |

这是我的代码,但它只是获得1个数组中的所有曲目,专辑,播放列表

return Yii::app()->db->createCommand()
        ->select('u.*,t.*,p.*,a.*')
        ->from('user u ')
        ->join('track t', 'u.user_id = s.user_id')
        ->leftjoin('playlist p', 'u.user_id = p.user_id')
        ->leftjoin('album a', 'u.user_id = a.user_id')
        ->andWhere('t.user_id ='.$id.' OR a.user_id ='.$id.' OR p.user_id ='.$id )
        ->queryAll(); 

我想要这个例子的结果:

[0] => array([album_id] => 11 , [user_id] => 1 , [album_title] => aaa ,[album_created] => 2014-06-03 )
[1] => array([song_id] => 13 , [user_id] => 1 , [song_title] => aaa ,[song_created] => 2014-06-02 )
[2] => array([song_id] => 22 , [user_id] => 1 , [song_title] => zyx ,[song_created] => 2014-05-30 )
[3] => array([playlist_id] => 1 , [user_id] => 1 , [playlist_title] => xxxx ,[playlist_created] => 2014-05-25 )

2 个答案:

答案 0 :(得分:0)

->select('u.*,t.*,p.*,a.*')
    ->from('user u ')
    ->join('track t', 'u.user_id = t.user_id')
    ->leftjoin('playlist p', 'u.user_id = p.user_id')
    ->leftjoin('album a', 'u.user_id = a.user_id')
    ->andWhere('t.user_id ='.$id.' AND u.user_id ='.$id.' AND a.user_id ='.$id.' AND p.user_id ='.$id )

- > order('a.album_created,p.plist_created,t.track_created')          - > queryAll();

答案 1 :(得分:0)

要获得所需的输出,您需要的是UNION曲目,播放列表和相册,如下所示:

   id   |   user_id   |   type   |   title   |   created   
--------|-------------|----------|-----------|-----------
在您的情况下,

type可以是albumplaylistsong。这可以使用CDbCommand::union()CDbCommand::getText()实现,如下所示:

return Yii::app()->db->createCommand()
    ->select("a.album_id, a.user_id, 'Album' as type, a.album_title, a.album_created as created")
    ->from('album a')
    ->andWhere('a.user_id ='.$id)
    ->union(
        Yii::app()->db->createCommand()
            ->select("s.song_id, s.user_id, 'Song' as type, s.song_title, s.song_created as created")
            ->from('song s')
            ->andWhere('s.user_id ='.$id)
            ->getText()
    )->union(
        Yii::app()->db->createCommand()
            ->select("p.playlist_id, p.user_id, 'Playlist' as type, p.playlist_title, p.plist_created as created")
            ->from('playlist p')
            ->andWhere('p.user_id ='.$id)
            ->getText()
    )->queryAll();

为了让生活更轻松,你应该从工会那里看一看。通过一些调整,您可以将CActiveRecord(以及relations()CActiveDataProvider)与此视图一起使用。