我有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
内选择,系统会因安全问题而拒绝。
谢谢。
答案 0 :(得分: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
答案 1 :(得分: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.我没有添加其他用户和位置条件,但您可以自己执行此操作。