這篇文章將為大家詳細(xì)講解有關(guān)MySQL中如何實(shí)現(xiàn)多表聯(lián)合查詢操作,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都做網(wǎng)站、成都網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的樂昌網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
MySQL多表聯(lián)合查詢語法:
復(fù)制代碼 代碼如下:
SELECT * FROM 插入表 LEFT JOIN 主表 ON t1.lvid=t2.lv_id select * from mytable,title where 表名1.name=表名2.writer ;
mysql版本大于4.0,使用UNION
進(jìn)行查詢,示例如下:
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中的相關(guān)記錄
復(fù)制代碼 代碼如下:
$sql = "CREATE TEMPORARY TABLE tmp_table_name SELECT `id`, `name`, `date`, '完成' AS `type` FROM table_A WHERE 條件語句……";
第二步:從table_B中取得相關(guān)記錄插入臨時表tmp_table_name中
復(fù)制代碼 代碼如下:
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區(qū)別分析
代碼示例:
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的列名不一樣時,會出現(xiàn)什么樣的結(jié)果?),這會對union后的結(jié)果集進(jìn)行排序。
修改為:
代碼示例:
(select * from test1 where name like 'A%' order by name) union (select * from test1 where name like 'B%' order by name)
也是可以的,這兩個order by在union前進(jìn)行。
2. 同樣
代碼示例:
select * from test1 where name like 'A%' limit 10 union select * from test1 where name like 'B%' limit 20
相當(dāng)于:
代碼示例:
(select * from test1 where name like 'A%' limit 10) union (select * from test1 where name like 'B%') limit 20
即后一個limit作用于的是union后的結(jié)果集,而不是union后的select。
也可以用括號括起來,以得到預(yù)期的結(jié)果:
3. UNION和UNION ALL區(qū)別
union會過濾掉union兩邊的select結(jié)果集中的重復(fù)的行,而union all不會過濾掉重復(fù)的行。
代碼示例:
(select * from test1 where name like 'A%' limit 10) union (select * from test1 where name like 'B%' limit 20)
下面試一個年齡段分析的復(fù)雜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') )
關(guān)于mysql中如何實(shí)現(xiàn)多表聯(lián)合查詢操作就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。