如何在sql中创建嵌套选择查询
我想做的是为网站创建一个评论部分,
评论由用户名、电子邮件和评论组成。我将这些数据存储在“评论”表中,
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`)
)
我想做的是执行一个查询来获取所有这些数据,同时检查“用户”表中的电子邮件地址以查看它是否存在。如果是,请从“杂项”表中获取头像。如果“用户”表中不存在该电子邮件,则将其留空。
目前我尝试查询,如果电子邮件存在于“用户”表中,它只会从 3 个表中获取数据。我还有另一条评论,作为匿名用户留下的,但查询没有抓住它。
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`)
)
我很确定我需要一个嵌套选择作为列名称,这样如果有电子邮件,它就会显示在那里......如果没有,它就会留空。
编辑:
使表结构保持应有的样子。
这是我刚刚尝试过的查询,但它只显示包含电子邮件地址的行。应该还有另一个没有电子邮件地址的
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
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' 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果我正确理解您的问题,问题在于您在
comments
和users
之间使用 INNER JOIN,这意味着它只会返回电子邮件中的匹配行。这就是为什么它不返回没有电子邮件地址或不匹配电子邮件地址的评论的原因。将 INNER JOIN 替换为 LEFT JOIN。尝试这个查询:
希望能帮助您获得所有评论。
If I correctly understood your issue, the problem is that you are using an INNER JOIN between
comments
andusers
, 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:
Hope that should help you get all comments.
不太确定您想要的输出是什么,如何为给定用户获得正确的杂项,但这是总体思路,
这是一个更具可读性的版本
请提供清晰的表格列表以及所需输出的示例,我们可以更好协助。
最终所需的示例输出在设计 MySQL 语句时非常有帮助。
Not really sure what your desired output, how you get the right misc for a given user, but here is the general idea
this is a more readable version
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.
不太确定这是否是您的意思,但我想您只是想在查询结果中添加一些额外的列,其中包含电子邮件地址(如果不可用则为空)和头像(如果不可用则为空),如果是的话,您可以使用左加入。
请注意,您使用的列名非常奇怪且不一致;仅使用名称 id 作为列的 id,并仅引用其他模型中的这些 id。
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.
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.
不,您真正需要的是
LEFT OUTER JOIN
。它的目的正是您所需要的 - 当在某个键上连接两个(或三个)表并且左表没有对应的键时,它的列在该键的结果集中填充为 NULL。
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.