如何加入两个查询? [英] How to join two queries?

查看:149
本文介绍了如何加入两个查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有两张桌子,分别是club和club_members。



表'club'包含'club_id'和'club_name',此表包含每个俱乐部ID和俱乐部名称

像这样:



 club_id club_name 
-------- ---------------
club1 clubName1

club2 clubName2





表'club_members'包含'club_id','user_id',此表包含用户ID和俱乐部ID

喜欢这个:



 club_id user_id 
-----------------------
club1 user1

club2 user1

club1 user2





如果用户搜索俱乐部名称,我必须显示俱乐部他没有加入的名字。



我试过这个查询,我不知道我在哪里做错了



从俱乐部选择不同的t1.club_id 
为t1
右加入

从club_members中选择club_id,其中user_id!='user1'

t2
t1.club_id = t2.club_id;





我希望表格中的上述数据不显示任何内容,但它会显示所有俱乐部名称。



我应该更改查询还是更改我的表格?我期待不要更改我的表。

解决方案

 选择 club_name 来自 club 其中 club_id    
选择 club_id 来自 club_members < span class =code-keyword>其中 user_id = ' user1'


http://www.w3schools.com/sql/ sql_join.asp [ ^ ]

http://www.w3schools.com/sql/sql_join_inner.asp [ ^ ]

http://www.aspdotnet-suresh.com/2012/07/self-join-query-in-sql-server-self-join.html [ ^ ]

http://www.c-sharpcorner.com/Forums/Thread/167480/how-to-use-inner-join-query-in-Asp-Net-using-C-Sharp.aspx [ ^ ]

http://beginner-sql-tutorial.com/sql-joins.htm [ ^ ]



试试吧..我可以..

I have two table in my database namely clubs and club_members.

table 'club' contains 'club_id' and 'club_name', this table contains each club id and club name
Like this:

club_id       club_name
-----------------------
club1         clubName1

club2         clubName2



table 'club_members' contains 'club_id', 'user_id', this table contains user ids and club ids
Like this:

club_id        user_id
-----------------------
club1          user1

club2          user1

club1          user2



If a user search for club names, I have to display the club names which he is not joined.

I have tried this query, I don't know where I am doing mistake

select distinct t1.club_id
from clubs as t1
right join
(
    select club_id from club_members where user_id != 'user1'
)
as t2
on t1.club_id = t2.club_id;



I am expecting nothing should display for the above data in the table, but it display all club names.

Should I change the query or change my table? I am expecting not to change my tables.

解决方案

select club_name from club where club_id not in
(select club_id from club_members where user_id = 'user1')


http://www.w3schools.com/sql/sql_join.asp[^]
http://www.w3schools.com/sql/sql_join_inner.asp[^]
http://www.aspdotnet-suresh.com/2012/07/self-join-query-in-sql-server-self-join.html[^]
http://www.c-sharpcorner.com/Forums/Thread/167480/how-to-use-inner-join-query-in-Asp-Net-using-C-Sharp.aspx[^]
http://beginner-sql-tutorial.com/sql-joins.htm[^]

Try it..u can..


这篇关于如何加入两个查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆