MySQL multi-Table Union query syntax example

Source: Internet
Author: User

MySQL multi-table joint query is a query method of MySQL database. The following describes the syntax of MySQL multi-table joint query for your reference.

MySQL multi-Table Union query syntax:

 
 
  1. SELECT * FROM Insert table left join main table ON t1.lvid = t2.lv _ id
  2.  
  3. Select * from mytable, title where table name 1. name = table name 2. writer;
  4.  

1. If mysql version is later than 4.0, use UNION for query. The example is as follows:

 
 
  1. SELECT 'id', 'name', 'date', ''as 'type' FROM table_A WHERE condition statement ......
  2. UNION
  3. SELECT 'id', 'name', 'date', 'unfinished 'AS 'type' FROM table_ B WHERE condition statement ......
  4. Order by 'id' LIMIT num;
  5.  

2. If mysql version is earlier than 4.0, you need to create a temporary table in three steps. The example is as follows:

Step 1: create a temporary table tmp_table_name and insert related records in table_A

 
 
  1. $ SQL = "CREATE TEMPORARY TABLE tmp_table_name SELECT 'id', 'name', 'date', 'complete the 'as 'type' FROM table_A WHERE Condition Statement ......";
  2. Mysql_query ($ SQL) OR die ("query false !");

Step 2: obtain related records from table_ B and insert them to the temporary table tmp_table_name.

 
 
  1. Mysql_query ("insert into tmp_table_name SELECT 'id', 'name', 'date2' AS 'date', 'unfinished 'as 'type' FROM table_ B WHERE Condition Statement ......") OR die ("query false !");

Step 3: retrieve records from the temporary table tmp_table_name

 
 
  1. mysql_query(”SELECT * FROM tmp_table_name ORDER BY id DESC”) or die (”query false!”);  

Alternative usage of MySQL table alias

MySQL left join query Experience Summary

How to display MYSQL table information

Three Common MySQL table creation statements

MySQL connection Query

Related Article

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.