Home > Backend Development > PHP Tutorial > 怎的同时查询多个数据表

怎的同时查询多个数据表

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 11:47:26
Original
733 people have browsed it

怎样同时查询多个数据表
有3个表(jtrb1,jtrb2,jtrb3),每个表都有几十万行记录,且每个表字段名称都一样(id,name,price,intime,outtime
怎样同时查询3个表里的内容?
比如我要在3个表里同时查字段name为“上衣”价格price小于“300”的记录。
求贴个PHP代码,单个表查询会做,多个表的就搞不动了。
另外:多表查询能不能用

while($row = mysql_fetch_array($result))
Copy after login

来输出内容?
最主要的还是贴个代码啊。谢谢啦!!
------解决方案--------------------
引用:
Quote: 引用:

单个
<br />$result1 = mysql_query("select * from jtrb1 where name='上衣' and price < 300");<br />$row1 = mysql_fetch_array($result1);<br /><br />$result2 = mysql_query("select * from jtrb2 where name='上衣' and price < 300");<br />$row2 = mysql_fetch_array($result1);<br /><br />$result3 = mysql_query("select * from jtrb3 where name='上衣' and price < 300");<br />$row3 = mysql_fetch_array($result3);<br />
Copy after login



组合
<br />select * from jtrb1,jtrb2,jtrb3 <br />where jtrb1.name='上衣' and jtrb1.price < 300<br />and   jtrb2.name='上衣' and jtrb2.price < 300<br />and   jtrb3.name='上衣' and jtrb3.price < 300<br />
Copy after login

组合这里,在数据库里查询,3个数据库里都有一条相同的记录,但它只列出一条,我想把三条都列出来,这里要怎么写?
还有
<?php <br />$con = mysql_connect("localhost","root","");<br />if (!$con)<br />  {<br />  die('Could not connect: ' . mysql_error());<br />  }<br />mysql_select_db("test", $con);<br />$query="SELECT * FROM jtrb1,jtrb2,jtrb3 WHERE jtrb1.name='上衣' and jtrb2.name='上衣' and jtrb3.name='上衣'";<br />$result= mysql_query($query,$con)or die(mysql_error());<br />$row= mysql_fetch_array($result);<br />while($row)<br />  {<br />	  echo $row['jtrb1.Name']."<br>";<br />	  //下面省略<br />  }<br />  mysql_close();<br />?>
Copy after login

我这样子写输出空白。请大神指点。



把数组打印出来。。用foreach试一试。
------解决方案--------------------
UNION试试
------解决方案--------------------

select * from (<br />select * from jtrb1 where jtrb1.name='上衣' and jtrb1.price < 300 <br />union all<br />select * from jtrb2 where jtrb2.name='上衣' and jtrb2.price < 300 <br />union all <br />select * from jtrb3 where jtrb3.name='上衣' and jtrb3.price < 300<br />) as t<br />
Copy after login

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template