日期:2014-05-16  浏览次数:20800 次

mysql关联查询语句如何写?
数据库:mysql

表A字段:id bides(存储值为 1,2,3,4,5,6...)
表B字段: id name

现在只知道A表中的ID值,请问如何循环列出A表该ID值对应的 B表集合!
也就是说给一个A表ID,然后列出B表中的内容(B表内容为:A表bides字段中的值)?????





------解决方案--------------------
SELECT * FROM A,B
where B.IdA.bides and A.id=1
order by B.id desc

------解决方案--------------------
SQL code
mysql> select * from a;
+------+----------+
| 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