简体   繁体   中英

7 query with inner join into 1 query, mysql

i have a question, how do i make totally 7 query into 1 query? to make the db query lesser? i have 1 table contain all of it, but the suggest is a "separator", each suggest i have to load 33 rows and order by dateline, i have think about use any inner join ... etc, but i think that is not a way? correct me if i'm wrong. Would Appreciate for help!THanks!!

This is the mysql query 1

$query = DB::query("SELECT t.*, d.did AS dingid, d.id AS dingpid, f.id AS bookmark, f.uid AS buid 
        FROM ".DB::table('comeing_tao')." AS t 
        LEFT JOIN ".DB::table('comeing_tao_ding')." AS d ON t.id = d.id AND d.uid = ".$_G['uid']." 
        LEFT JOIN ".DB::table('comeing_tao_fans')." AS f ON t.id = f.id AND f.uid = ".$_G['uid']." 
        WHERE t.suggest = 0 AND t.state = 1 ORDER BY dateline DESC LIMIT 33");

This is the mysql query 2

$query = DB::query("SELECT t.*, d.did AS dingid, d.id AS dingpid, f.id AS bookmark, f.uid AS buid 
            FROM ".DB::table('comeing_tao')." AS t 
            LEFT JOIN ".DB::table('comeing_tao_ding')." AS d ON t.id = d.id AND d.uid = ".$_G['uid']." 
            LEFT JOIN ".DB::table('comeing_tao_fans')." AS f ON t.id = f.id AND f.uid = ".$_G['uid']." 
            WHERE t.suggest = 1 AND t.state = 1 ORDER BY dateline DESC LIMIT 33");

This is the mysql query 3

$query = DB::query("SELECT t.*, d.did AS dingid, d.id AS dingpid, f.id AS bookmark, f.uid AS buid 
        FROM ".DB::table('comeing_tao')." AS t 
        LEFT JOIN ".DB::table('comeing_tao_ding')." AS d ON t.id = d.id AND d.uid = ".$_G['uid']." 
        LEFT JOIN ".DB::table('comeing_tao_fans')." AS f ON t.id = f.id AND f.uid = ".$_G['uid']." 
        WHERE t.suggest = 2 AND t.state = 1 ORDER BY dateline DESC LIMIT 33");

This is the mysql query 3

$query = DB::query("SELECT t.*, d.did AS dingid, d.id AS dingpid, f.id AS bookmark, f.uid AS buid 
        FROM ".DB::table('comeing_tao')." AS t 
        LEFT JOIN ".DB::table('comeing_tao_ding')." AS d ON t.id = d.id AND d.uid = ".$_G['uid']." 
        LEFT JOIN ".DB::table('comeing_tao_fans')." AS f ON t.id = f.id AND f.uid = ".$_G['uid']." 
        WHERE t.suggest = 3 AND t.state = 1 ORDER BY dateline DESC LIMIT 33");

This is the mysql query 4

$query = DB::query("SELECT t.*, d.did AS dingid, d.id AS dingpid, f.id AS bookmark, f.uid AS buid 
        FROM ".DB::table('comeing_tao')." AS t 
        LEFT JOIN ".DB::table('comeing_tao_ding')." AS d ON t.id = d.id AND d.uid = ".$_G['uid']." 
        LEFT JOIN ".DB::table('comeing_tao_fans')." AS f ON t.id = f.id AND f.uid = ".$_G['uid']." 
        WHERE t.suggest = 4 AND t.state = 1 ORDER BY dateline DESC LIMIT 33");

and so on... i skip the following 3 query cos is totally same except the t.suggest = 5, t.suggest = 6, t.suggest = 7

the goal is all the query in one, then play around with the array.

you can use OR

(t.suggest=3 OR t.suggest=4 OR ...) AND ...

if you want max 33 records from each suggest, try UNION

(SELECT ...) UNION (SELECT ...) UNION (SELECT ...) ...

TRY in clause

SELECT 
    t.*, d.did AS dingid, d.id AS dingpid, f.id AS bookmark, f.uid AS buid 
FROM 
    ".DB::table('comeing_tao')." AS t 
LEFT JOIN ".DB::table('comeing_tao_ding')." AS d ON t.id = d.id AND d.uid = 
     ".$_G['uid']." 
LEFT JOIN ".DB::table('comeing_tao_fans')." AS f ON t.id = f.id AND f.uid = 
     ".$_G['uid']." 
WHERE 
      t.suggest IN(0,1,2,3,4,5,6,7) 
AND 
      t.state = 1 
ORDER BY 
      dateline DESC 

but Limit will not give 33 records for each but 33 for whole return

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM