简体   繁体   English

SQL 查询返回空,我不知道为什么?

[英]SQL query returning empty and I can't figure out why?

I have this complicated query that technically works, but recently the "developer_" part of it changed from required to optional.我有这个在技术上有效的复杂查询,但最近它的“developer_”部分从必需更改为可选。 Now when that particular data is missing, the query returns empty.现在,当缺少特定数据时,查询返回空。

    $sql = "SELECT    a.*,
                      b.title AS manufacturer_title,
                      b.slug AS manufacturer_slug,
                      b.url AS manufacturer_url,
                      c.fname AS manufacturer_logo,
                      d.title AS region_title,
                      e.title AS game_title,
                      e.slug AS game_slug,
                      f.title AS developer_title,
                      f.slug AS developer_slug,
                      f.url AS developer_url,
                      g.fname AS developer_logo,
                      h.title AS exclusive_title,
                      h.slug AS exclusive_slug,
                      h.url AS exclusive_url,
                      i.fname AS exclusive_logo,
                      j.title AS edition_title,
                      k.title AS edition_parent_title
            FROM      catalog_items AS a
            JOIN      catalog_franchises AS b ON a.manufacturer_id = b.franchise_id
            LEFT JOIN catalog_images AS c ON b.logo_id = c.img_id
            JOIN      catalog_regions AS d ON a.region_id = d.region_id
            JOIN      catalog_franchises AS e ON a.game_id = e.franchise_id
            JOIN      catalog_franchises AS f ON a.developer_id = f.franchise_id
            LEFT JOIN catalog_images AS g ON f.logo_id = g.img_id
            LEFT JOIN catalog_franchises AS h ON a.exclusive_id = h.franchise_id
            LEFT JOIN catalog_images AS i ON h.logo_id = i.img_id
            LEFT JOIN catalog_editions AS j ON a.edition_id = j.edition_id
            LEFT JOIN catalog_editions AS k ON j.parent_id = k.edition_id
            WHERE     a.item_id = :item_id
                      AND a.valid = TRUE
                      AND b.valid = TRUE
                      AND (c.valid = TRUE OR c.fname IS NULL)
                      AND d.valid = TRUE
                      AND e.valid = TRUE
                      AND f.valid = TRUE
                      AND (g.valid = TRUE OR g.fname IS NULL)
                      AND (h.valid = TRUE OR h.title IS NULL)
                      AND (i.valid = TRUE OR i.fname IS NULL)
                      AND (j.valid = TRUE OR j.title IS NULL)
                      AND (k.valid = TRUE OR k.title IS NULL)
            GROUP BY  a.item_id";

I have tried modifying AND f.valid = TRUE to various things:我尝试将AND f.valid = TRUE修改为各种内容:

AND (f.valid = TRUE OR f.title IS NULL)

AND (f.valid = TRUE OR a.developer_id IS NULL)

But nothing seems to do the trick.但似乎没有什么能解决问题。 What do I need to modify so that all references to "developer_" can be optional?我需要修改什么以便对“developer_”的所有引用都是可选的?

Let's tidy up your JOIN and WHERE stuff and use one more LEFT JOIN.让我们整理一下您的 JOIN 和 WHERE 内容,然后再使用一个 LEFT JOIN。 By putting the x.valid = TRUE items in the ON conditions things get a little cleaner in the OR... IS NULL department.通过将x.valid = TRUE项目置于 ON 条件下, OR... IS NULL部门的事情变得更加清晰。

And to keep rows in the resultset where catalog_franchises.franchise_id doesn't match any catalog_items.developer_id change out your JOIN for a LEFT JOIN.并在结果集中保留catalog_items.developer_id catalog_franchises.franchise_id匹配的行,将您的 JOIN 更改为 LEFT JOIN。

SELECT      a.*, b.many columns, c.many columns ... etc ...
  FROM      catalog_items AS a
  JOIN      catalog_franchises AS b     ON a.manufacturer_id = b.franchise_id
                                       AND b.valid = TRUE
  LEFT JOIN catalog_images AS c         ON b.logo_id = c.img_id
                                       AND c.valid = TRUE
  JOIN      catalog_regions AS d        ON a.region_id = d.region_id
                                       AND d.valid = TRUE
  JOIN      catalog_franchises AS e     ON a.game_id = e.franchise_id
                                       AND e.valid = TRUE
  /* use LEFT JOIN on this next line to allow developer_id to not match */
  LEFT JOIN catalog_franchises AS f     ON a.developer_id = f.franchise_id
                                       AND f.valid = TRUE
  LEFT JOIN catalog_images AS g         ON f.logo_id = g.img_id
                                       AND g.valid = TRUE
  ... etc etc etc  ...
  WHERE a.item_id = :item_id
    AND a.valid = TRUE
  GROUP BY  a.item_id  /* this wrongly chooses arbitrary values from aliases b-k */

You're misusing MySQL's notorious nonstandard extension to GROUP BY .您正在滥用 MySQL 臭名昭著的对 GROUP BY 的非标准扩展 It may cause some confusion in your result set when you start getting results.当您开始获取结果时,它可能会导致结果集中出现一些混乱。

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

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