开发者

How to create a nested select query in sql

What I am trying to do is create a comments section for a website,

The comments consist of a user's name, email and comment. I store this data in the 'comments' table

CREATE TABLE `comments` (
  `commentid` int(5) NOT NULL auto_increment,
  `user` varchar(40) NOT NULL default '',
  `email` varchar(100) NOT NULL default '',
  `comment` text NOT NULL,
  PRIMARY KEY  (`commentid`)
)

What i want to do is execute a query that grabs all this data but also checks the email address in the 'users' table to see if it exists. If it does, grab the avatar from the 'misc开发者_C百科' table. If the email doesn't exist in the 'users' table, it's just left blank.

At the moment with the query i tried, it only grabs the data from the 3 tables if the email exists in the 'users' table. I have another comment which as anonymous user left but that's not getting grabbed by the query.

CREATE TABLE `users` (
  `userid` int(25) NOT NULL auto_increment,
  `email` varchar(255) NOT NULL default '',
  `username` varchar(25) NOT NULL default '',
  PRIMARY KEY  (`userid`)
)

CREATE TABLE `misc` (
  `miscid` int(4) NOT NULL auto_increment,
  `userid` varchar(3) NOT NULL default '',
  `avatar` varchar(100) NOT NULL default '',
  PRIMARY KEY  (`miscid`)
)

I am pretty sure i need a nested select as a column name so that if there is an email it displays there...if not it's left blank.

EDIT:

Made the table structures how it should be.

This is a query I have just tried but it only displays a row which has an email address. there should be another without email address

SELECT c.comment, c.user, av.avatar
FROM comments c
INNER JOIN users u ON c.email = u.email
LEFT OUTER JOIN (

SELECT userid, avatar
FROM misc
) AS av ON av.userid = u.userid


If I correctly understood your issue, the problem is that you are using an INNER JOIN between comments and users, which means that it will only return matching rows on email. Thus the reason why it does not return comments that are without email addresses or non-matching email addresses.

Replace your INNER JOIN with a LEFT JOIN. Try out this query:

SELECT `c`.`comment`, `c`.`user`, `m`.`avatar`
FROM `comments` `c`
LEFT JOIN `users` `u` ON `c`.`email` = `u`.`email`
LEFT JOIN `misc` `m` ON `m`.`userid` = `u`.`userid`;

Hope that should help you get all comments.


Not really sure what your desired output, how you get the right misc for a given user, but here is the general idea

SELECT userid, email, username, IF(email<>'',(SELECT avatar from misc where miscid = users.userid),null) avater FROM users;

this is a more readable version

SELECT
  userid,
  email,
  username,
  IF(email<>''
    ,/*then*/(SELECT avatar from misc where miscid = users.userid)
    ,/*else*/null)
   as avater
FROM users;

Please provide a clear list of your tables, and an example desired output, and we can better assist.

The final desired example output is very helpful when designing MySQL statements.


SELECT * FROM comments LEFT JOIN users   
ON users.email=comments.email 


Not really sure if this is what you mean, but I guess you just want some extra columns in your query result with the email address (empty if not available) and avatar (empty if not available), if that's right you can work with a LEFT JOIN.

SELECT
   c.*,
   u.email,
   m.avatar
FROM
   comments as c LEFT JOIN
   users as u ON (u.userid = c.user) LEFT JOIN
   misc as m ON (m.miscid = u.userid)

Please not that the column names you are using are quite weird and inconsistent; use just the name id for the id of the column, and reference only to those id's in other models.


No, what you actually need is LEFT OUTER JOIN. Its purpose is exactly what you need - when joining two (or three) tables on some key and the left table has no correspondent key it's columnsare filled with NULL in the result set for that key.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜