Mysql三表查询问题

艾派德迷你2 2009-11-04 05:24:10
遇到的情况如下

有三个表..分别为 tt_user,tt_group,tt_config
tt_user 有tt_id,tt_name,tt_check
tt_group有gg_id
tt_config有cc_id

我现在的目的是..
要从tt_user中查出条件为tt_user.tt_id!=tt_group.gg_id and tt_user.tt_check<tt_config.cc_id

尝试用下面的方法...
select * from tt_user right join (tt_group,tt_config) on (tt_user.tt_id!=tt_group.gg_id and tt_user.tt_check<tt_config.cc_id)
select * from tt_user inner join (tt_group,tt_config) on (tt_user.tt_id!=tt_group.gg_id and tt_user.tt_check<tt_config.cc_id)
查出来的结果
总是有重复的...
意思就是说

查出来的是..先按照tt_user.tt_id!=tt_group.gg_id来查...然后又按照tt_user.tt_check<tt_config.cc_id
来查
然后结果等于两者之和...

有没有其他比较好的..查询速度快的方法...
麻烦介绍一下....

哎..本人是菜鸟一个....
...全文
169 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
kaada 2009-11-05
  • 打赏
  • 举报
回复
楼主的表,会产生重复,其实就是关联条件不够,产生迪卡约乘积,但没有明显的表结构,在例题上也非常模糊,没办法进行解答,我举这么个例子吧,
TABLE_A.TB1 = TABLE_B.CB1,
TABLE_A.TB2 = TABLE_C.DB1,
TABLE_B和TABLE_C没有纪录产生交集的情况下,就以上两个条件就可以成立
但是,如果是
TABLE_A.TB1 = TABLE_B.CB1 = TABLE_C.DB1,
那么,对等要三个条件
也就是
TABLE_A.TB1 = TABLE_B.CB1
AND
TABLE_B.CB1 = TABLE_C.DB1
AND
TABLE_A.TB1 = TABLE_C.DB1
这么说,应该明白吧
艾派德迷你2 2009-11-05
  • 打赏
  • 举报
回复
如果还没有看明白
我用下面的数据来说话咯
三个表: rcfa_user,rcfa_group,rcfa_config

rcfa_user的表里面的东西

mysql> select User_id,User_firstname,User_type
from rcfa_user where User_enable=2;
+---------+----------------+-----------+
| User_id | User_firstname | User_type |
+---------+----------------+-----------+
| 9 | Kaka | 99 |
| 3 | Weifu | 3 |
| 4 | Maggie | 1 |
| 5 | Anna | 1 |
+---------+----------------+-----------+
4 rows in set

rcfa_group里面的

mysql> select Group_members from rcfa_group;
+---------------+
| Group_members |
+---------------+
| 3 |
| 9 |
+---------------+

rcfa_config里面的

mysql> select Group_admin from rcfa_config;
+-------------+
| Group_admin |
+-------------+
| 4 |
+-------------+

现在要查出来的东西
+---------+----------------+-----------+
| User_id | User_firstname | User_type |
+---------+----------------+-----------+
| 4 | Maggie | 1 |
| 5 | Anna | 1 |
+---------+----------------+-----------+
艾派德迷你2 2009-11-05
  • 打赏
  • 举报
回复
测试数据如下

tt_user,tt_group,tt_config

tt_user:

tt_id tt_name tt_check
1 a 1
2 b 4
3 c 1
4 d 1

tt_group:

gg_id
3


tt_config
cc_id
4

查询条件为: 从tt_user中查询去 tt_id!=tt_group.gg_id 并且 tt_check<tt_config.cc_id


要查出的结果为...
tt_id tt_name
1 a
4 d

用right join查询出来的结果就是

tt_id tt_name
1 a
2 b
4 d
1 a
3 c
4 d
艾派德迷你2 2009-11-05
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 acmain_chm 的回复:]
SQL code测试数据如下

tt_user,tt_group,tt_config

tt_user:

tt_id tt_name tt_check1 a12 b43 c14 d1

tt_group:

gg_id3


tt_config
cc_id4

查询条件为: 从tt_user中查询去 tt_id!=tt_group.gg_id 并且 tt_check<tt_config.cc_id

select * from tt_user
where tt_id not in (select gg_id from tt_group)
and tt_check <(select cc_id from tt_config);
[/Quote]

非常感谢...搞定咯...嘿嘿
renadg 2009-11-05
  • 打赏
  • 举报
回复


select * from tt_user
where tt_id!=(select gg_id from tt_group)
and tt_check<(select cc_id from tt_config)


查询结果:
-----------------------
tt_id tt_name tt_check
1 a 1
4 d 1

-----------------------

ACMAIN_CHM 2009-11-05
  • 打赏
  • 举报
回复
测试数据如下

tt_user,tt_group,tt_config

tt_user:

tt_id tt_name tt_check
1 a 1
2 b 4
3 c 1
4 d 1

tt_group:

gg_id
3


tt_config
cc_id
4

查询条件为: 从tt_user中查询去 tt_id!=tt_group.gg_id 并且 tt_check <tt_config.cc_id


select * from tt_user
where tt_id not in (select gg_id from tt_group)
and tt_check<(select cc_id from tt_config);
ACMAIN_CHM 2009-11-04
  • 打赏
  • 举报
回复
没看懂!

建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。

56,687

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧