這篇文章將為大家詳細(xì)講解有關(guān)MySQL中如何實(shí)現(xiàn)多表聯(lián)合查詢操作,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的樂昌網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(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,需要建立臨時(shí)表,分為三步,示例如下:
第一步:建立臨時(shí)表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)記錄插入臨時(shí)表tmp_table_name中
復(fù)制代碼 代碼如下:
INSERT INTO tmp_table_name SELECT `id`, `name`, `date2` AS `date`, '未完成' AS `type` FROM table_B WHERE 條件語句……
第三步:從臨時(shí)表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. 以下查詢會(huì)報(bào)錯(cuò)誤:[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中,不用括號(hào)的情況下,只能用一個(gè)order by(思考:union兩邊的order by的列名不一樣時(shí),會(huì)出現(xiàn)什么樣的結(jié)果?),這會(huì)對(duì)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)
也是可以的,這兩個(gè)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
即后一個(gè)limit作用于的是union后的結(jié)果集,而不是union后的select。
也可以用括號(hào)括起來,以得到預(yù)期的結(jié)果:
3. UNION和UNION ALL區(qū)別
union會(huì)過濾掉union兩邊的select結(jié)果集中的重復(fù)的行,而union all不會(huì)過濾掉重復(fù)的行。
代碼示例:
(select * from test1 where name like 'A%' limit 10) union (select * from test1 where name like 'B%' limit 20)
下面試一個(gè)年齡段分析的復(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)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)頁名稱:mysql中如何實(shí)現(xiàn)多表聯(lián)合查詢操作
新聞來源:http://jinyejixie.com/article28/gpgdcp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、自適應(yīng)網(wǎng)站、建站公司、電子商務(wù)、定制網(wǎng)站、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)