How to query multiple data tables at the same time

Source: Internet
Author: User
How to query multiple data tables at the same time with three tables (jtrb1, jtrb2, jtrb3), each table has hundreds of thousands of rows of records, and each table field name is the same (id, name, price, intime, outtime) how to query the content of the three tables at the same time? For example, I want to query records with the field name "coat" price less than "300" in three tables. Paste a PHP code. a single table will be queried, and multiple tables will not work. In addition, how can I query multiple data tables at the same time?
There are three tables (jtrb1, jtrb2, jtrb3), each with hundreds of thousands of rows of records, and each table field name is the same (id, name, price, intime, outtime)
How can I query the content in three tables at the same time?
For example, I want to query records with the field name "coat" price less than "300" in three tables.
Paste a PHP code. a single table will be queried, and multiple tables will not work.
In addition, can multi-table queries be used?
while($row = mysql_fetch_array($result))

To output content?
The most important thing is to post code. Thank you !!

Share: More


------ Solution --------------------
Reference:
Quote: reference:

Single

$ Result1 = mysql_query ("select * from jtrb1 where name = 'coat 'and price <300 ");
$ Row1 = mysql_fetch_array ($ result1 );

$ Result2 = mysql_query ("select * from jtrb2 where name = 'coat 'and price <300 ");
$ Row2 = mysql_fetch_array ($ result1 );

$ Result3 = mysql_query ("select * from jtrb3 where name = 'coat 'and price <300 ");
$ Row3 = mysql_fetch_array ($ result3 );



Combination

Select * from jtrb1, jtrb2, jtrb3
Where jtrb1.name = 'coat 'and jtrb1.price <300
And jtrb2.name = 'coat 'and jtrb2.price <300
And jtrb3.name = 'coat 'and jtrb3.price <300

In combination, the query in the database contains the same record in all three databases, but it only lists one record. I want to list all three records. how can I write them here?
And
   $ Con = mysql_connect ("localhost", "root ","");
If (! $ Con)
{
Die ('could not connect: '. mysql_error ());
}
Mysql_select_db ("test", $ con );
$ Query = "SELECT * FROM jtrb1, jtrb2, jtrb3 WHERE jtrb1.name = 'coat 'and jtrb2.name = 'coat' and jtrb3.name = 'coat '";
$ Result = mysql_query ($ query, $ con) or die (mysql_error ());
$ Row = mysql_fetch_array ($ result );
While ($ row)
{
Echo $ row ['jtrb1. name']."
";
// Skipped below
}
Mysql_close ();
?>

I write the output blank like this. Please kindly advise.



Print out the array .. Use foreach to try it.
------ Solution --------------------
Try UNION
------ Solution --------------------

Select * from (
Select * from jtrb1 where jtrb1.name = 'coat 'and jtrb1.price <300
Union all
Select * from jtrb2 where jtrb2.name = 'coat 'and jtrb2.price <300
Union all
Select * from jtrb3 where jtrb3.name = 'coat 'and jtrb3.price <300
) As t

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.