有三个表。
A表三字段 id(唯一ID) a_id a_name B表三字段 id(唯一ID) b_id c_id
想通过查询 B 表 b_id 得到变量$c_id=c_id;
再通过$c_id等于A表中的a_id 查询出a_name;相当于SQL中出现两个循环。SQL怎么写?跪求。紧急
A表三字段 id(唯一ID) a_id a_name B表三字段 id(唯一ID) b_id c_id
想通过查询 B 表 b_id 得到变量$c_id=c_id;
再通过$c_id等于A表中的a_id 查询出a_name;相当于SQL中出现两个循环。SQL怎么写?跪求。紧急
解决方案 »
- 各位帮忙找找句法错误,鼓捣了半天不行
- phpmyadmin无法连接到mysql服务器1045
- 这个cookie好奇怪!@
- 如何设置Apache+PHP+Mysql,显示utf-8、gb2312等编码形式!
- header()貌似没有跳转 求帮助
- 配置服务器时出现 :oracle.dll失败-找不到指定的模块。
- 请问linux下的php编辑工具有哪些,谢谢
- mysql的类型区别(谁有相关的文章!我不懂什么时候用int 什么时候用bigint )!:)
- 怎么根据微信id代码自动生成生成关注微信公众号二维码图片
- header location 的页面跳转问题
- 分步提交表单,如何实现上一步,下一步的功能?在线等!!!
- php+sqlserver 2000 连接
没看清要求,应该这样。
SELECT A.a_name FROM A,B WHERE A.a_id=B.b_id
SELECT A.a_name FROM A where a_id='$b_id' $b_id' 是从B表中查询出来的,是一个循环。比如 B表有十条记录。我循环出 $b_id' 是B表中3 、7 、9 三条记录(一个循环)
现在要通过得出的3、7 、9 查询在A表中a_id为 3 、7 、9 的所有记录。
from A inner join B on A.a_id=B.c_id
where B.b_id=123
+ -------+------------------+--------+
| ID a_id a_name |
| 1 7 name_one |
| 2 3 name_two |
| 3 9 name_three |
| 4 8 name_four |
+------------------------------------+
table2
|+ -------+------------------+--------+
| ID b_id b_value |
| 1 7 x |
| 2 3 x |
| 3 9 x |
| 4 8 y |
+-------------------------------------+<?php
header("Content-Type:text/html;charset=utf-8");require_once ("connect.php");
mysql_select_db('test');
$sql=mysql_query("select a_id,a_name from table1 where a_id in (select b_id from table2 where b_value='x');",$connect);
mysql_error($sql);
$arr=array();
while ($row=mysql_fetch_array($sql))
{
$arr[]=$row;
}
echo '<pre>';
print_r($arr);
echo '</pre>';
?>
结果:
Array
(
[0] => Array
(
[0] => 7
[a_id] => 7
[1] => name_one
[a_name] => name_one
) [1] => Array
(
[0] => 3
[a_id] => 3
[1] => name_two
[a_name] => name_two
) [2] => Array
(
[0] => 9
[a_id] => 9
[1] => name_three
[a_name] => name_three
))
不知道你是不是要的这样的效果?