+ -
当前位置:首页 → 问答吧 → MYSQL 的二次查询怎么写?

MYSQL 的二次查询怎么写?

时间:2011-10-20

来源:互联网

表test
title code
123 456
123 789
444 012

1 先查出 code=456
2 再查出 code=456 的title 的值
3 根据2查询出的title值 最後查出 表test 中所有title 和code 值

作者: tianya2297360   发布时间: 2011-10-20

什么意思?几个表?为啥要查询这么多次。

select title,code from test where code = '456';
这个就可以获得所有的code为456 的title了。

select * from test;
这个可以获得所有的title和code

作者: ohmygirl   发布时间: 2011-10-20

我也没看懂什么意思

作者: cpc1984   发布时间: 2011-10-20

select title,code from test where title = (select title from test where code = '456');

作者: chendong_j   发布时间: 2011-10-20

引用 3 楼 chendong_j 的回复:

select title,code from test where title = (select title from test where code = '456');


你这是完全多余的子查询。

作者: ohmygirl   发布时间: 2011-10-20

不对啊
查询不出来

作者: tianya2297360   发布时间: 2011-10-20

select title,code from test where title = (select title from test where code = '456');
正解

作者: tianya2297360   发布时间: 2011-10-20

引用 6 楼 tianya2297360 的回复:

select title,code from test where title = (select title from test where code = '456');
正解

是正解么?
如果有多个code = 456 的,那么用=还对么?即使用子查询也要用.

select title,code from test where title in (select title from test where code = '456');

作者: ohmygirl   发布时间: 2011-10-20