php - USING SELECT CASE based on a constant value -
i have ff table called equipment:
+----------+----------+-----------+-------------+----------+---------+ | equip_id | chara_id | weapon_id | headgear_id | armor_id | ring_id | +----------+----------+-----------+-------------+----------+---------+ | 3 | 1 | 3 | 5 | 9 | 8 | | 5 | 3 | 3 | 5 | 3 | 8 | | 6 | 4 | 7 | 5 | 3 | 8 | | 7 | 5 | 4 | 5 | 3 | 8 | | 8 | 6 | 3 | 5 | 2 | 8 | | 10 | 8 | 3 | 5 | 2 | 8 | +----------+----------+-----------+-------------+----------+---------+
and have long case statement:
switch ($equip->item_type) { case '1': # weapon $sql_equip = "update equipment set weapon_id = :item_id chara_id = :chara_id"; break; case '2': # armor $sql_equip = "update equipment set armor_id = :item_id chara_id = :chara_id"; break; case '3': # ring $sql_equip = "update equipment set ring_id = :item_id chara_id = :chara_id"; break; case '4': # headgear $sql_equip = "update equipment set headgear_id = :item_id chara_id = :chara_id"; break; default: # nothing... break; }
i saw queries on net can use case statements in queries, ive searched net , cant seem figure out correct 1 use.
i want in 1 statement since wrap on transact function , add line of mysql statement. instead of doing 4x2(current) 2(if done in 1 query) mysql queries.
you can have statement,
update equipment set weapon_id = case when :itemtype = 1 :item_id else weapon_id end, armor_id = case when :itemtype = 2 :item_id else armor_id end, ring_id = case when :itemtype = 3 :item_id else ring_id end, headgear_id = case when :itemtype = 4 :item_id else headgear_id end chara_id = :chara_id
but need pass 3 values, :itemtype
, :item_id
, :chara_id
Comments
Post a Comment