您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關mysql中如何實現多表聯合查詢操作,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
MySQL多表聯合查詢語法:
復制代碼 代碼如下:
SELECT * FROM 插入表 LEFT JOIN 主表 ON t1.lvid=t2.lv_id select * from mytable,title where 表名1.name=表名2.writer ;
mysql版本大于4.0,使用UNION
進行查詢,示例如下:
SELECT `id`, `name`, `date`, '' AS `type` FROM table_A WHERE 條件語句…… UNION SELECT `id`, `name`, `date`, '未完成' AS `type` FROM table_B WHERE 條件語句…… ORDER BY `id` LIMIT num;
mysql版本小于4.0,需要建立臨時表,分為三步,示例如下:
第一步:建立臨時表tmp_table_name并插入table_A中的相關記錄
復制代碼 代碼如下:
$sql = "CREATE TEMPORARY TABLE tmp_table_name SELECT `id`, `name`, `date`, '完成' AS `type` FROM table_A WHERE 條件語句……";
第二步:從table_B中取得相關記錄插入臨時表tmp_table_name中
復制代碼 代碼如下:
INSERT INTO tmp_table_name SELECT `id`, `name`, `date2` AS `date`, '未完成' AS `type` FROM table_B WHERE 條件語句……
第三步:從臨時表tmp_table_name中取出記錄
SELECT * FROM tmp_table_name ORDER BY id DESC
union和order by、limit區別分析
代碼示例:
CREATE TABLE `test1` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, `desc` varchar(100) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
1. 以下查詢會報錯誤:[Err] 1221 - Incorrect usage of UNION and ORDER BY
代碼示例:
select * from test1 where name like 'A%' order by name union select * from test1 where name like 'B%' order by name
修改為:
代碼示例:
select * from test1 where name like 'A%' union select * from test1 where name like 'B%' order by name
說明,在union中,不用括號的情況下,只能用一個order by(思考:union兩邊的order by的列名不一樣時,會出現什么樣的結果?),這會對union后的結果集進行排序。
修改為:
代碼示例:
(select * from test1 where name like 'A%' order by name) union (select * from test1 where name like 'B%' order by name)
也是可以的,這兩個order by在union前進行。
2. 同樣
代碼示例:
select * from test1 where name like 'A%' limit 10 union select * from test1 where name like 'B%' limit 20
相當于:
代碼示例:
(select * from test1 where name like 'A%' limit 10) union (select * from test1 where name like 'B%') limit 20
即后一個limit作用于的是union后的結果集,而不是union后的select。
也可以用括號括起來,以得到預期的結果:
3. UNION和UNION ALL區別
union會過濾掉union兩邊的select結果集中的重復的行,而union all不會過濾掉重復的行。
代碼示例:
(select * from test1 where name like 'A%' limit 10) union (select * from test1 where name like 'B%' limit 20)
下面試一個年齡段分析的復雜sql語句
( SELECT '5~19' AS `age`, SUM(`impression`) AS impression, SUM(`click`) AS click, sum(`cost`) AS cost FROM `adgroup_age_report` WHERE ( ( (`age` <= 19) AND (`adgroup_id` = '61') ) AND (`date` >= '2015-11-22') ) AND (`date` <= '2017-02-20') ) UNION ( SELECT '20~29' AS `age`, SUM(`impression`) AS impression, SUM(`click`) AS click, sum(`cost`) AS cost FROM `adgroup_age_report` WHERE ( ( ((`age` <= 29) AND(`age` >= 20)) AND (`adgroup_id` = '61') ) AND (`date` >= '2015-11-22') ) AND (`date` <= '2017-02-20') ) UNION ( SELECT '30~39' AS `age`, SUM(`impression`) AS impression, SUM(`click`) AS click, sum(`cost`) AS cost FROM `adgroup_age_report` WHERE ( ( ((`age` <= 39) AND(`age` >= 30)) AND (`adgroup_id` = '61') ) AND (`date` >= '2015-11-22') ) AND (`date` <= '2017-02-20') ) UNION ( SELECT '40~49' AS `age`, SUM(`impression`) AS impression, SUM(`click`) AS click, sum(`cost`) AS cost FROM `adgroup_age_report` WHERE ( ( ((`age` <= 49) AND(`age` >= 40)) AND (`adgroup_id` = '61') ) AND (`date` >= '2015-11-22') ) AND (`date` <= '2017-02-20') ) UNION ( SELECT '50~59' AS `age`, SUM(`impression`) AS impression, SUM(`click`) AS click, sum(`cost`) AS cost FROM `adgroup_age_report` WHERE ( ( ((`age` <= 59) AND(`age` >= 50)) AND (`adgroup_id` = '61') ) AND (`date` >= '2015-11-22') ) AND (`date` <= '2017-02-20') )
關于mysql中如何實現多表聯合查詢操作就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。