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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?