简体   繁体   中英

select multiple tables in single sql query

The other threads about this didn't seem to help me. I want to select all the information from one table, but order them by a value in another table.

SELECT message, 
    DATE, 
    ip, 
    name, 
    website, 
    id 
FROM   guestbook_message 
WHERE  deleted = 0 
    AND DATE > Date_sub(Now(), interval 1 day) 
ORDER  BY DATE DESC

Except I need to ORDER BY 'votes' DESC; which is in another table called m_votes .

Is it possible to do this? I have read on another website that this query is impossible.

$query="SELECT g.message, 
           g.DATE, 
           g.ip, 
           g.name, 
           g.website, 
           g.id 
    FROM   guestbook_message AS g 
           join m_votes AS v 
             ON g.id = v.vid 
    WHERE  g.deleted = 0 
           AND v.messageid = $mid 
           AND g.DATE > Date_sub(Now(), interval 1 day) 
    ORDER  BY SUM(v.votes) DESC;"

^^This doesn't work

You need a join:

SELECT
     g.message,
     g.date,
     g.ip,
     g.name,
     g.website,
     g.id 
FROM guestbook_message AS g
LEFT JOIN m_votes AS v
ON g.id = v.message_id
WHERE g.deleted = 0 
AND g.date > NOW() - INTERVAL 1 DAY
GROUP BY g.id
ORDER BY COUNT(v.message_id) DESC

I'm a bit of a beginner and to me this is a very hard query as it needs to find the SUM of 'votes' in m_votes and order by that. As well as get the information from the other query. This is the query that gets the information about the message:

 "SELECT message,  
         `date`,  
         ip,  
         name,  
         website,  
         id   
  FROM `guestbook_message`   
  WHERE deleted = 0   
     AND date > DATE_SUB(NOW(), INTERVAL 1 DAY)   
  ORDER BY `date` DESC";  

And this is the query that gets the information about the votes:

"SELECT SUM(votes) as votes FROM m_votes WHERE messageid = $mid"

But I have no idea who I would put them into one query that will gather all the information from the first query, then ORDER them by votes.

You have to join the data, that is, you need to have the votes for each guestbook message.

Let's suppose for simplicity you have the following tables:

Message
----
id INT
messsageText VARCHAR(5000)

and

MessageVotes
------------
messageId INT (references the `id` column in table Message)
voteValue INT (suppose it can be +1 or -1, whatever)
votingIp  VARCHAR(100)

Then you could do something like

SELECT
  m.id,
  m.messageText,
  SUM(mv.voteValue) AS votes
FROM
  Message AS m,
  MessageVotes AS mv
WHERE
  mv.messageId = m.id
GROUP BY
  m.id, m.messageText /* here you need to place every field you `select` from Message */
ORDER BY
  SUM(mv.voteValue) DESC

or even better:

SELECT
  m.id,
  m.messageText,
  SUM(mv.voteValue) AS votes
FROM
  Message AS m
  LEFT JOIN MessageVotes AS mv ON mv.messageId = m.id
GROUP BY
  m.id, m.messageText
ORDER BY
  SUM(mv.voteValue) DESC

See:

Please try below query, you missed group by clause.

    SELECT g.message, 
        g.date, 
        g.ip, 
        g.name, 
        g.website, 
        g.id 
    FROM guestbook_message AS g 
    JOIN m_votes AS v ON g.id = v.vid 
    WHERE g.deleted = 0 
        AND v.messageid = $mid 
        AND g.date > DATE_SUB(NOW(), INTERVAL 1 DAY) 
    GROUP BY g.message, 
        g.date, 
        g.ip, 
        g.name, 
        g.website, 
        g.id
    ORDER BY SUM(v.votes) DESC;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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