sql join left聯合關聯查詢實例教程
a表: 結果與數據
studentID workID store
1 1 98
1 2 95
2 1 90
2 3 80
b表: 結果與數據
workID workName
1 zuoye
2 www.bKjia.c0m
3 wenti
我們要用join left聯合查詢結果為了如下
studentID workID workName store
1 1 zuoye 98
1 2 shiti 95
或者
studentID workID workName store
2 1 zuoye 98
2 3 wenti 80
*/
$sql ='select a.studentid,a.workid,b.workname from a join b on a.workid=b.workid where a.studentid=1 ';
$sql2 =' select a.studentid,a.workid,b.workname,a.store from a join b on a.workid=b.workid where a.studentid=1 select a.studentid,a.workid,b.workname,a.store from a join b on a.workid=b.workid where a.studentid=2 ';
//數據庫教程連接
$db ='fangke_cc';
mysql教程_pconnect('localhost','root','root') or die(mysql_error());
mysql_select_db($db);
mysql_query("set names 'gbk'");
$query = mysql_query($sql);
while( $rs = mysql_fetch_array( $query ) )
{
echo $rs[0],$rs[1],$rs[2];
}
/*
結果為www.bKjia.c0m
studentID workID workName store
1 1 zuoye 98
1 2 www.bKjia.c0m 95
*/
$query = mysql_query($sql1);
while( $rs = mysql_fetch_array( $query ) )
{
echo $rs[0],$rs[1],$rs[2];
}
/*
結果為
studentID workID workName store
2 1 zuoye 98
2 3 wenti 80