如果ID存在于另一个表中,则拉行

时间:2015-08-06 02:40:25

标签: php mysql if-statement left-join union-all

我构建了这个查询来从两个表wp_sshow_sales + tickets和live_15中提取所有数据,它对此非常有用。但我希望它只能从wp_sshow_sales&如果在wp_sshow_verifys.verifyID

中找到wp_sshow_sales.saleID,则显示票证

正如您所看到的,我尝试过使用IF和LEFT JOIN,但我无法指出这一点。我在IF语句的某处出现错误。

SELECT 
    wp_sshow_sales.saleFirstName as first,
    wp_sshow_sales.saleLastName as last,
    wp_sshow_sales.saleEMail as email,
    wp_sshow_sales.salePPStreet as street,
    wp_sshow_sales.salePPCity as city,
    wp_sshow_sales.salePPState as state,
    wp_sshow_sales.salePPZip as zip,
    wp_sshow_tickets.ticketQty as qty
    IF (wp_sshow_verifys.verifyID IS NULL,TRUE,FALSE) verify
    FROM wp_sshow_sales, wp_sshow_tickets 
    WHERE wp_sshow_sales.saleID = wp_sshow_tickets.saleID
    LEFT JOIN verify ON wp_sshow_sales.saleID = wp_sshow_verifys.verifyID

    UNION ALL
    SELECT
    live_15.firstname as first,
    live_15.lastname as last,
    live_15.email as email,
    live_15.street as street,
    live_15.city as city,
    live_15.state as state,
    live_15.zip as zip,
    live_15.qty as qty
    FROM live_15

    ORDER BY $order ASC

1 个答案:

答案 0 :(得分:0)

您的syntax声明中出现IF错误,因为您之前错过了comma

SELECT 
    wp_sshow_sales.saleFirstName as first,
    ...
    wp_sshow_tickets.ticketQty as qty, <-----
    IF (wp_sshow_verifys.verifyID IS NULL,TRUE,FALSE) verify
    FROM wp_sshow_sales, wp_sshow_tickets 
    WHERE wp_sshow_sales.saleID = wp_sshow_tickets.saleID
    LEFT JOIN verify ON wp_sshow_sales.saleID = wp_sshow_verifys.verifyID

我猜你的WHERE条款也会有错误,应该在你的JOIN陈述之后写下来。

如果你想从wp_sshow_sales&amp; wp_sshow_tickets仅当saleID中找到wp_sshow_verifys时,请使用INNER JOIN代替LEFT JOIN

有关JOIN的更多信息,请check this SO question