2016-04-10 93 views
2

我有2個表,game_jnship_equip_list是商店設備基本信息,game_jnship_equip是存儲爲什麼玩家有。使用左連接但值不顯示?

這裏是game_jnship_equip_list數據:

+--------+----------------+----------+----------+ 
| ID  | desc   | name  | type  | 
+--------+----------------+----------+----------+ 
| 1  | hello_weapon | weapon | 1  | 
| 2  | hello_shirt | shirt | 2  | 
| 3  | hell_weapon | Hweapon | 1  | 
+-----------------------------------------------+ 

這裏是game_jnship_equip數據:

+------+----------+--------------+------------+------------+ 
| ID | userid | itemcode  | atfigure | eposition | 
+------+----------+--------------+------------+------------+ 
| 1 | 1  | 1   | 100  | 1   | 
| 2 | 1  | 2   | 500  | 2   | 
+----------------------------------------------------------+ 

我查詢如下:

$quequip = DB::query("SELECT t1.*,t2.name AS weaponame, t2.edesc AS weapondesc, t3.atfigure AS atkbonus, t4.name AS shirtname, t4.edesc AS shirtdesc FROM ".DB::table('game_jnship_equip')." t1 LEFT JOIN ".DB::table('game_jnship_equip_list')." t2 ON (t1.itemid = t2.id AND t1.eposition = '1') LEFT JOIN ".DB::table('game_jnship_equip')." t3 ON (t2.type = t3.eposition) LEFT JOIN ".DB::table('game_jnship_equip_list')." t4 ON (t1.itemid = t4.id AND t1.eposition = '2') WHERE t1.uid = 'userid' AND t1.status = '1'"); 
$ruequip = DB::fetch($quequip); 

但是,我只能得到如下值:

$ruequip['weaponame'] = weapon; 
$ruequip['weapodesc'] = hello_weapon; 
$ruequip['atkbonus'] = 100; 

然後關於t4全部空白。

$ruequip['shirtname'] = ; 
$ruequip['shirtdesc'] = ; 

我想讓它顯示如下值:

$ruequip['shirtname'] = shirt; 
$ruequip['shirtdesc'] = hello_shirt; 

那麼,如何解決這一問題?和我的DB::query功能,它不能允許DB::query(SELECT * FROM xxxx (SELECT * FROM)),手段2選擇內部1 DB::query,系統會因安全問題拒絕。

謝謝。

回答

2

可以使用的CASE EXPRESSION代替3左聯接有條件聚集做到這一點:

SELECT s.id,s.userid,s.itemcode,s.atfigure,s.eposition, 
     MAX(CASE WHEN s.eposition = 1 THEN s.desc END) as weap_desc, 
     MAX(CASE WHEN s.eposition = 1 THEN s.name END) as weap_name, 
     MAX(CASE WHEN s.eposition = 2 THEN s.desc END) as shirt_desc, 
     MAX(CASE WHEN s.eposition = 2 THEN s.name END) as shirt_name 
FROM (SELECT t.id,t.userid,t.itemcode,t.atfigure,t.eposition,t2.desc,t2.name 
     FROM ".DB::table('game_jnship_equip')." t 
     LEFT OUTER JOIN ".DB::table('game_jnship_equip_list')." t2 
     ON(t1.itemid = t2.id AND t.eposition = t2.type))s 
GROUP BY s.user_id 
+0

對不起,我的'DB :: query'函數不允許2'select * from'函數的安全問題 –

+0

@SweeHong那你爲什麼用它呢?無論如何現在嘗試。 – sagi

1

爲什麼不是工會?

SELECT 
    list.id as weaponid, 
    list.desc as weapondesc, 
    list.name as weaponame, 
    quip.atfigure as atkbonus 
from ".DB::table('game_jnship_equip_list')." as list 
    left join ".DB::table('game_jnship_equip')." as quip 
    on (quip.itemcode = list.id) 
Where list.type = 1 
UNION 
SELECT 
    list.id as shirtid, 
    list.desc as shirtdesc, 
    list.name as shirtname, 
    quip.atfigure as atkbonus 
from elist as list 
    left join equip as quip 
    on (quip.itemcode = list.id) 
Where list.type = 2 

這將讓你特定類型2和類型特定1.我沒有添加其他用戶和位置條件,但你可以這樣做你自己。

+0

你爲什麼要這樣做?它與1同樣用'WHERE list.type in(1,2)'選擇。不要談論他的要求是讓這個列彼此相鄰,而不是作爲單獨的行。 – sagi