如何在sql中创建嵌套选择查询

发布于 2024-11-16 05:45:42 字数 1223 浏览 1 评论 0原文

我想做的是为网站创建一个评论部分,

评论由用户名、电子邮件和评论组成。我将这些数据存储在“评论”表中,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

江南月 2024-11-23 05:45:42

如果我正确理解您的问题,问题在于您在 commentsusers 之间使用 INNER JOIN,这意味着它只会返回电子邮件中的匹配行。这就是为什么它不返回没有电子邮件地址或不匹配电子邮件地址的评论的原因。

将 INNER JOIN 替换为 LEFT JOIN。尝试这个查询:

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`;

希望能帮助您获得所有评论。

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.

剪不断理还乱 2024-11-23 05:45:42

不太确定您想要的输出是什么,如何为给定用户获得正确的杂项,但这是总体思路,

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

这是一个更具可读性的版本

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

请提供清晰的表格列表以及所需输出的示例,我们可以更好协助。

最终所需的示例输出在设计 MySQL 语句时非常有帮助。

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.

病女 2024-11-23 05:45:42
SELECT * FROM comments LEFT JOIN users   
ON users.email=comments.email 
SELECT * FROM comments LEFT JOIN users   
ON users.email=comments.email 
南汐寒笙箫 2024-11-23 05:45:42

不太确定这是否是您的意思,但我想您只是想在查询结果中添加一些额外的列,其中包含电子邮件地址(如果不可用则为空)和头像(如果不可用则为空),如果是的话,您可以使用左加入。

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)

请注意,您使用的列名非常奇怪且不一致;仅使用名称 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.

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.

帥小哥 2024-11-23 05:45:42

不,您真正需要的是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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文