开发者

mysql with many tables

开发者 https://www.devze.com 2023-02-05 06:05 出处:网络
I have a major problem getting this to work properly. Here is the deal. I have 4 tables - timeline, connections, images, votes

I have a major problem getting this to work properly. Here is the deal.

I have 4 tables - timeline, connections, images, votes

Timeline table:

time_id
time_title

etc.

The timeline table connects with the images table via the connections table

Connections table:

time_id
image_id

So the connections hold information about the id of the "post" the timeline and about image id

Images table:

image_id
image_url
image_type

Images table contains images that could be used in the same timeline(post), so by this i can see the connection by looking at the connection table.

I also have a votes table that holds information about votes for the specific timeline post

Votes table:

vote_id
time_id

the votes table contains many rows of each time_id and the idea is to count the number of times to se wich is the most popular

So heres the question: I need to get the 5 most voted time_titles and show them with the first image for each time_title(post) so that it look like this for example

  • Image 1 that corresponds to Title 1 - that i开发者_如何学运维s the highest voted
  • Image 2 that corresponds to Title 2 - that is the second highest voted

etc.

Each timeline row (post) can hold many images and i only want to show the first of each post.

Hope you understand my problem!

Thanks a lot!


The query will be

SELECT
 count(v.vote_id) as vote_times
 t.time_title,
 ( 
  SELECT
   image_url
  FROM
   images 
  WHERE
   image_id = (
       SELECT
        image_id
       FROM
        connections as c
       WHERE
        c.time_id = t.time_id
       LIMIT 1
      )
  LIMIT 1
 ) as image_url
FROM
 votes as v
  LEFT JOIN
   time as t 
    on 
     t.time_id = v.time_id
GROUP BY
 v.time_id
ORDER BY
 vote_times desc
LIMIT 5;

This will work, but might not be the most optimized one.

-- EDIT to get image_type also try

SELECT
    count(v.vote_id) as vote_times
    t.time_title,
 i.image_url,
 i.image_type

FROM
    votes as v
        LEFT JOIN
            time as t 
                on 
                    t.time_id = v.time_id
  LEFT JOIN
   images as i
    on
     i.image_id = (
         SELECT
          image_id
         FROM
          connections as c
         WHERE
          c.time_id = t.time_id
         LIMIT 1
        )
GROUP BY
    v.time_id
ORDER BY
    vote_times desc, image_type desc
LIMIT 5;


SELECT  t.time_title, i.*
FROM    (
        SELECT  t.time_title
        FROM    timeline t
        LEFT JOIN
                votes v
        ON      v.time_id = t.time_id
        GROUP BY
                t.id
        ORDER BY
                COUNT(vote_id) DESC, t.time_id DESC
        LIMIT 5
        ) t
JOIN    images i
ON      i.id = 
        (
        SELECT  ii.image_id
        FROM    connections c
        JOIN    image ii
        ON      ii.image_id = c.image_id
        WHERE   c.time_id = t.time_id
        ORDER BY
                ii.id DESC -- or whatever order you define for the "first" image
        LIMIT 1
        )
0

精彩评论

暂无评论...
验证码 换一张
取 消