数据库:mysql表A字段:id bides(存储值为 1,2,3,4,5,6...)
表B字段: id name现在只知道A表中的ID值,请问如何循环列出A表该ID值对应的 B表集合!
也就是说给一个A表ID,然后列出B表中的内容(B表内容为:A表bides字段中的值)?????
表B字段: id name现在只知道A表中的ID值,请问如何循环列出A表该ID值对应的 B表集合!
也就是说给一个A表ID,然后列出B表中的内容(B表内容为:A表bides字段中的值)?????
where Id in(SELECT A.bides FROM A where A.id ='1')
order by B.id desc我是这样写的 但报错了
where B.IdA.bides and A.id=1
order by B.id desc
where B.Id in(A.bides) and A.id=1
order by B.id desc
这样可正常运行,但一次只显示不条记录,并不能循环获取!!
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
应是少个等号呗SELECT * FROM A,B
where B.Id=A.bides and A.id=1
order by B.id descLZ 要是要一系列的,那就用 inSELECT * FROM A,B
where B.Id=A.bides and A.id in (1, 2, 6, 8, 10)
order by B.id desc
id varchar(10),
BIdes varchar(20)
)create table B(
id varchar(10),
name varchar(20)
)
A表值为:
id Bides
1 1,3,4,5
2 2,4,8,10B表值为:
id name
1 A
2 B
3 C
4 D
5 E/*
要得到这样的表(传递一个A表ID,获取对应的B表name值):
例如:给A表id传递1,则显示B表列表内容:
id name
1 A
3 C
4 D
5 E
*/
+------+----------+
| id | BIdes |
+------+----------+
| 1 | 1,3,4,5 |
| 2 | 2,4,8,10 |
+------+----------+
2 rows in set (0.00 sec)mysql> select * from b;
+------+------+
| id | name |
+------+------+
| 1 | A |
| 2 | B |
| 3 | C |
| 4 | D |
| 5 | E |
+------+------+
5 rows in set (0.00 sec)mysql> select b.*
-> from A,b
-> where a.id=1 And find_in_set(B.id,a.BIdes);
+------+------+
| id | name |
+------+------+
| 1 | A |
| 3 | C |
| 4 | D |
| 5 | E |
+------+------+
4 rows in set (0.02 sec)mysql>
select b.* from b left join b on instr(a.BIdes,B.id)>0 where a.id=1