简体   繁体   English

mysql - 子查询和连接

[英]mysql - subqueries and joins

I'm not quite sure if this is the right approach, this is my situation: 我不太确定这是否是正确的方法,这是我的情况:

I'm currently trying to select 15 galleries and then left join it with the user table through the id but I also want to select one random picture from each gallery however from what I know you can't limit the left join (picture) to only pick up one random picture without doing a subquery. 我目前正在尝试选择15个画廊,然后通过id将用户表连接起来,但我也想从每个画廊中选择一张随机图片,但据我所知,你不能将左连接(图片)限制为只拍摄一张随机图片而不做子查询。

Here is what I got so far but its not working as it should: 这是我到目前为止所得到的,但它不能正常工作:

SELECT galleries.id, galleries.name, users.username, pictures.url 
FROM galleries 
LEFT JOIN users ON users.id = galleries.user_id 
LEFT JOIN pictures ON (
    SELECT pictures.url 
    FROM pictures 
    WHERE pictures.gallery_id = galleries.id 
    ORDER BY RAND() 
    LIMIT 1) 
WHERE active = 1 
ORDER BY RAND() 
LIMIT 15

I also tried to do this with Active Record but I got stuck after doing two left joins, is it possible to do get a subquery in here: 我也尝试使用Active Record执行此操作但是在执行两次左连接后我遇到了问题,是否可以在此处获取子查询:

$this->db->select('galleries.id, galleries.name, users.id as user_id, users.username');
$this->db->from('galleries');
$this->db->join('users', 'users.id = galleries.user_id','left');
$this->db->join('pictures','pictures.gallery_id = galleries.id AND','left');
$this->db->where('active',1);

I hope its not to messy but I'm really starting to get confusing by all the sql queries.. 我希望它不要乱,但我真的开始让所有的SQL查询混淆..

Edit: Active Record with CodeIgniter 编辑: 使用CodeIgniter进行活动记录

You could fetch a random picture in a subquery: 您可以在子查询中获取随机图片:

select 
    g.name, u.username,
    (select url from pictures p where p.gallery_id = g.gallery_id 
     order by rand() limit 1) as url
from galleries g
left join users u on g.user_id = u.id
where g.active = 1

Based on your comment, you could select a picture for each gallery in a subquery. 根据您的评论,您可以为子查询中的每个图库选择一张图片。 This is assuming the picture table has an ID column. 这假设图片表具有ID列。

select 
    g.name, u.username, p.url, p.name
from (
    select id, user_id, name,
        (select id from pictures p 
         where p.gallery_id = g.gallery_id 
         order by rand() limit 1) as samplepictureid
    from galleries
    where g.active = 1
) g
left join users u on g.user_id = u.id
left join pictures p on p.id = g.samplepictureid
SELECT
    g.id,
    g.name,
    u.username,
    p.url
FROM
    galleries g
    INNER JOIN (SELECT DISTINCT
         gallery_id,
         (SELECT url FROM pictures ss WHERE ss.gallery_id = s.gallery_id 
             ORDER BY RAND() LIMIT 1) AS url
     FROM
         pictures s) p ON
        g.id = p.gallery_id
    LEFT OUTER JOIN users u ON
        g.user_id = u.id
WHERE
    g.active = 1

This query will go out and select a gallery, then it will find any gallery with a picture (if you want to return galleries without a picture, change INNER JOIN to LEFT OUTER JOIN, and you'll be fine). 此查询将出去并选择一个图库,然后它会找到任何带有图片的图库(如果您想要返回没有图片的图库,请将INNER JOIN更改为LEFT OUTER JOIN,您就可以了)。 After that, it joins it up with users. 之后,它与用户加入。 Now, of course, this puppy is going to return every frigging gallery for however many users you have (hoorah!). 当然,现在,这只小狗将返回每个嬉戏画廊,无论你有多少用户(hoorah!)。 You may want to limit the user in the WHERE clause (eg- WHERE u.id = 123 ). 您可能希望在WHERE子句中限制用户(例如, WHERE u.id = 123 )。 Otherwise, you're going to get more results than you'd expect. 否则,你将获得比你期望的更多的结果。 That, or do an INNER JOIN on it. 那,或者做一个INNER JOIN就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM