doumi0737 2018-08-03 10:47
浏览 76
已采纳

SQL - 如果不为null,则选择不同表中的列

I am trying to make a "recipe" system inside a game. The player can own a company and craft items in there.

I currently fetch the recipes per company type but I don't know how to write the query in a way that I can also fetch the item names and images if the item_id is not empty.

This is working:

SELECT a.recipe_id, 
        a.item1_id, 
        a.item1_uses,
        a.item2_id, 
        a.item2_uses, 
        a.item3_id, 
        a.item3_uses, 
        a.item4_id, 
        a.item4_uses, 
        a.item5_id, 
        a.item5_uses, 
        a.newitem_id, 
        a.newitem_uses, 
        a.craft_description, 
        a.craft_button
    FROM
        company_recipes AS a, 
        company_types AS b
    WHERE
        a.type_id = b.type_id 
    AND
        b.type_id = '".$type."'; 
    "

A recipe can contain for example two items needed to craft something new, but it could also be 5. So if it's only 2, I only want to fetch the img, name of these 2 and the rest can be skipped.

I have a different table store_items that contains the img and name of the item. I was thinking something along the lines of an IF ELSE or CASE WHEN inside the query, but I'm not sure how I'd do that.

Something like: SELECT c.img, c.name FROM store_items AS c IF a.item1_id is not NULL.

I feel like I'm close to the solution, but missing the last step.

  • 写回答

2条回答 默认 最新

  • duanbairan4235 2018-08-03 11:29
    关注

    Thanks for the tips @jarlh, I've changed the code and came to this result. If you have any more tips to do it better I'm happy to listen. (I'm still a junior and thought myself by trial and error, so I might not have the best solutions at times... Which is why tips are highly appreciated).

    SELECT cr.recipe_id, 
            cr.item1_id, 
            cr.item1_uses,
            si1.name,
            si1.img,
            cr.item2_id, 
            cr.item2_uses,
            si2.name,
            si2.img,
            cr.item3_id, 
            cr.item3_uses, 
            si3.name,
            si3.img,
            cr.item4_id, 
            cr.item4_uses,
            si4.name,
            si4.img, 
            cr.item5_id, 
            cr.item5_uses,
            si5.name,
            si5.img, 
            cr.newitem_id, 
            cr.newitem_uses,
            si_new.name,
            si_new.img,
            cr.craft_description, 
            cr.craft_button
        FROM
            company_recipes AS cr
            INNER JOIN company_types AS ct ON cr.type_id = ct.type_id
            LEFT JOIN store_items AS si1 ON cr.item1_id = si1.item_id
            LEFT JOIN store_items AS si2 ON cr.item2_id = si2.item_id
            LEFT JOIN store_items AS si3 ON cr.item3_id = si3.item_id
            LEFT JOIN store_items AS si4 ON cr.item4_id = si4.item_id
            LEFT JOIN store_items AS si5 ON cr.item5_id = si5.item_id
            LEFT JOIN store_items AS si_new ON cr.newitem_id = si_new.item_id
        WHERE
            ct.type_id = '".$type."';
    

    I'm basically fetching everything now and handle the NULLs in the php code now.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持