简体   繁体   English

在 MySql 中使用 IF 或 CASE 和 INNER JOIN

[英]Using IF or CASE with INNER JOIN in MySql

I've got 4 different user (punter) tables (terrible idea but massive redesign needed a long time ago) that I need to list users from a particular event.我有 4 个不同的用户(投注者)表(糟糕的想法,但很久以前需要进行大规模的重新设计),我需要列出来自特定事件的用户。

I've looked at this post but no joy MySQL query where JOIN depends on CASE我看过这篇文章,但不高兴 MySQL 查询其中 JOIN 取决于 CASE

I've also had a look through similar questions but haven't found anything I can use.我也看过类似的问题,但没有找到任何我可以使用的东西。

Here's my query:这是我的查询:

SELECT 
    sale_id, sale_punter_type, sale_refund, sale_timestamp, 
    sale_punter_type,


    AES_DECRYPT(punter_email, :keyPunter) AS punter_email,
    punter_firstname,
    punter_surname,
    AES_DECRYPT(punter_postcode, :keyPunter) AS punter_postcode,


    AES_DECRYPT(punter_checkout_email, :keyPunter) AS punter_checkout_email,
    punter_checkout_firstname,
    punter_checkout_surname,
    AES_DECRYPT(punter_checkout_postcode, :keyPunter) AS punter_checkout_postcode,


    AES_DECRYPT(punter_compo_email, :keyPunter) AS punter_compo_email,
    punter_compo_firstname,
    punter_compo_surname,


    AES_DECRYPT(punter_shop_email, :keyPunter) AS punter_shop_email,
    punter_shop_firstname,
    punter_shop_surname

FROM 
    sale 
INNER JOIN
    ticket_sold ON sale_id = ticket_sold_sale_no
INNER JOIN
    event ON event_id = ticket_sold_event_no
INNER JOIN 


    (CASE
        WHEN 
            sale_punter_type = 1 THEN punter ON punter_id = sale_punter_no 
        WHEN 
            sale_punter_type = 2 THEN punter_checkout ON punter_checkout_id = sale_punter_no 
        WHEN 
            sale_punter_type = 3 THEN punter_compo ON punter_compo_id = sale_punter_no 
        WHEN 
            sale_punter_type = 4 THEN punter_shop ON punter_shop_id = sale_punter_no 
    END) 


WHERE 
    event_id = :id

I tried using LEFT JOIN:我尝试使用左连接:

SELECT 
    sale_id, sale_punter_type, sale_refund, sale_timestamp, 
    sale_punter_type,


    AES_DECRYPT(punter_email, :keyPunter) AS punter_email,
    punter_firstname,
    punter_surname,
    AES_DECRYPT(punter_postcode, :keyPunter) AS punter_postcode,


    AES_DECRYPT(punter_checkout_email, :keyPunter) AS punter_checkout_email,
    punter_checkout_firstname,
    punter_checkout_surname,
    AES_DECRYPT(punter_checkout_postcode, :keyPunter) AS punter_checkout_postcode,


    AES_DECRYPT(punter_compo_email, :keyPunter) AS punter_compo_email,
    punter_compo_firstname,
    punter_compo_surname,


    AES_DECRYPT(punter_shop_email, :keyPunter) AS punter_shop_email,
    punter_shop_firstname,
    punter_shop_surname

FROM 
    sale 
INNER JOIN
    ticket_sold ON sale_id = ticket_sold_sale_no
INNER JOIN
    event ON event_id = ticket_sold_event_no
INNER JOIN 
    punter ON punter_id = sale_punter_no 
LEFT JOIN 
    punter_checkout ON punter_checkout_id = sale_punter_no 
LEFT JOIN 
    punter_compo ON punter_compo_id = sale_punter_no 
LEFT JOIN 
    punter_shop ON punter_shop_id = sale_punter_no 
WHERE 
    event_id = :id

but it returned users with the same ID but from different tables.但它返回了具有相同 ID 但来自不同表的用户。

I'm not sure how to explain it, I need something like我不知道如何解释它,我需要类似的东西

IF sale_punter_no = 1 THEN INNER JOIN punter ON punter_id = sale_punter_no

Hopefully that makes sense and mysql can do such things希望这是有道理的,mysql 可以做这样的事情

Thank you谢谢

You can perform LEFT JOIN on each table as shown below.您可以对每个表执行LEFT JOIN ,如下所示。

However, from your example it's not clear which columns do you want to use from each related table;但是,从您的示例中,您不清楚要从每个相关表中使用哪些列; you'll need to use COALESCE() or similar to merge them accordingly.您需要使用COALESCE()或类似方法来相应地合并它们。 it's always a good pactice to prepend each column with the table (or table alias) it belongs to.在每一列前面加上它所属的表(或表别名)总是一个很好的做法。

Here's the query:这是查询:

SELECT 
    sale_id, sale_punter_type, sale_refund, sale_timestamp, 
    sale_punter_type,

    AES_DECRYPT(punter_email, :keyPunter) AS punter_email,
    punter_firstname,
    punter_surname,
    AES_DECRYPT(punter_postcode, :keyPunter) AS punter_postcode,

    AES_DECRYPT(punter_checkout_email, :keyPunter) AS punter_checkout_email,
    punter_checkout_firstname,
    punter_checkout_surname,
    AES_DECRYPT(punter_checkout_postcode, :keyPunter) AS punter_checkout_postcode,

    AES_DECRYPT(punter_compo_email, :keyPunter) AS punter_compo_email,
    punter_compo_firstname,
    punter_compo_surname,

    AES_DECRYPT(punter_shop_email, :keyPunter) AS punter_shop_email,
    punter_shop_firstname,
    punter_shop_surname

FROM sale 
JOIN ticket_sold ON sale_id = ticket_sold_sale_no
JOIN event ON event_id = ticket_sold_event_no

LEFT JOIN punter ON punter_id = sale_punter_no AND sale_punter_type = 1
LEFT JOIN punter_checkout ON punter_checkout_id = sale_punter_no AND sale_punter_type = 2
LEFT JOIN punter_compo ON punter_compo_id = sale_punter_no AND sale_punter_type = 3
LEFT JOIN punter_shop ON punter_shop_id = sale_punter_no AND sale_punter_type = 4
WHERE event_id = :id

Your IF/CASE statements need to be in the SELECT.您的 IF/CASE 语句需要在 SELECT 中。 So, start with your LEFT JOIN query.因此,从您的 LEFT JOIN 查询开始。 Try this for your select:为您的 select 试试这个:

SELECT 
    sale_id, sale_punter_type, sale_refund, sale_timestamp, 
    sale_punter_type,

    CASE sale_punter_type
      WHEN 1 THEN AES_DECRYPT(punter_email, :keyPunter)
      WHEN 2 THEN AES_DECRYPT(punter_checkout_email, :keyPunter)
      WHEN 3 THEN AES_DECRYPT(punter_compo_email, :keyPunter)
      WHEN 4 THEN AES_DECRYPT(punter_shop_email, :keyPunter)
    END punter_email,
    CASE sale_punter_type
      WHEN 1 THEN punter_firstname
      WHEN 2 THEN punter_checkout_firstname
      WHEN 3 THEN punter_compo_firstname
      WHEN 4 THEN punter_shop_firstname
    END punter_firstname,
    CASE sale_punter_type
      WHEN 1 THEN punter_surname
      WHEN 2 THEN punter_checkout_surname
      WHEN 3 THEN punter_compo_surname
      WHEN 4 THEN punter_shop_surname
    END punter_surname,
    CASE sale_punter_type
      WHEN 1 THEN AES_DECRYPT(punter_postcode, :keyPunter)
      WHEN 2 THEN AES_DECRYPT(punter_checkout_postcode, :keyPunter)
      ELSE NULL
    END punter_postcode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM