在MYSQL中使用联接

时间:2012-11-13 21:04:31

标签: mysql join

我有以下一组带有附加查询的表。显然,我填充了太多AND条款,现在没有结果有效,即使是结果。下面显示的是我正在使用的图表和SQL。

MySQL Database Model-hidden fields aren't vital.

SELECT p.products_id, pd.language_id, pd.products_name,
       pd.products_description, pd.products_url, p.products_quantity,
       p.products_model, p.products_image, p.products_price, p.products_virtual,
       p.products_weight, p.products_date_added, p.products_last_modified,
       p.products_date_available, p.products_status, p.manufacturers_id,
       p.products_quantity_order_min, p.products_quantity_order_units, p.products_priced_by_attribute,
       p.product_is_free, p.product_is_call, p.products_quantity_mixed,
       p.product_is_always_free_shipping, p.products_qty_box_status, p.products_quantity_order_max, p.products_sort_order, 

FROM `magez_products` p, `magez_products_description` pd
       WHERE p.products_id = pd.products_id
       AND p.products_id = 186
       AND p.products_id = cfv.products_id 
       AND cfv.nation_id = cfvn.nation_id
       AND cfv.clan_id = cfvc.clan_id
       AND cfv.rarity_id = cfvr.rarity_id

4 个答案:

答案 0 :(得分:1)

我在这些情况下通常做的是从较小的查询开始(例如一个AND)并且越来越多地增加查询,直到你看到它开始有“无结果”的地方。否则调试它非常困难。

答案 1 :(得分:1)

这不是完整的选择或mysql应该抱怨,因为选择缺少cfv,cfvn,cfvc和cfvr表。

您可以通过从and ...子句的末尾逐个删除where来解决此问题,并查看结果再次开始包含行的位置。

例如,一个问题可能是联接表(例如pd,cfv)缺少products_id

另一种解决方案可能是通过左连接替换内连接,具体取决于您的要求。

答案 2 :(得分:1)

您需要加入声明

SELECT p.products_id, pd.language_id, pd.products_name,
   pd.products_description, pd.products_url, p.products_quantity,
   p.products_model, p.products_image, p.products_price, p.products_virtual,
   p.products_weight, p.products_date_added, p.products_last_modified,
   p.products_date_available, p.products_status, p.manufacturers_id,
   p.products_quantity_order_min, p.products_quantity_order_units,         
p.products_priced_by_attribute,
   p.product_is_free, p.product_is_call, p.products_quantity_mixed,
   p.product_is_always_free_shipping, p.products_qty_box_status,                                                                               p.products_quantity_order_max,
p.products_sort_order, 

FROM `magez_products` p
join `magez_products_description` pd
on p.products_id = pd.products_id
   AND p.products_id = 186
   AND p.products_id = cfv.products_id      <-------------- what is cfv  join   on that too !!!!!!!!!!
   AND cfv.nation_id = cfvn.nation_id
   AND cfv.clan_id = cfvc.clan_id
   AND cfv.rarity_id = cfvr.rarity_id

答案 3 :(得分:1)

为了帮助,我也是老式的ANSI格式......你会发现在表之间使用JOINS编写的更多当前查询,并且只在你想要“限制”的地方应用“AND”。有时,这可能直接在连接而不是主“from”表。此外,通过作为连接,您可以确切地看到表“A”如何连接到表“B”,并且我尝试将A = B比较保持在与列出的表相同的顺序中...左侧表上=左侧,=右侧的右侧表格,如

SELECT 
      p.products_id, 
      pd.language_id, 
      pd.products_name,
      pd.products_description, 
      pd.products_url, 
      p.products_quantity,
      p.products_model, 
      p.products_image, 
      p.products_price, 
      p.products_virtual,
      p.products_weight, 
      p.products_date_added, 
      p.products_last_modified,
      p.products_date_available, 
      p.products_status, 
      p.manufacturers_id,
      p.products_quantity_order_min, 
      p.products_quantity_order_units, 
      p.products_priced_by_attribute,
      p.product_is_free, 
      p.product_is_call, 
      p.products_quantity_mixed,
      p.product_is_always_free_shipping, 
      p.products_qty_box_status, 
      p.products_quantity_order_max, 
      p.products_sort_order
   FROM 
      magez_products p
         JOIN magez_products_description pd
            on p.products_id = pd.products_id
         JOIN magez_cfv_cards cfv
            on p.products_id = cfv.products_id
            JOIN magez_cfv_nations cfvn
               on cfv.nation_id = cfvn.nation_id
            JOIN magez_cfv_clans cfvc
               on cfv.clan_id = cfvc.clan_id
            JOIN magez_cfv_rarity cfvr
               on cfv.rarity_id = cfvr.rarity_id
   where
      p.products_id = 186

注意我的格式,它与你的表格结构有关... A加入B,B加入各自列的C,D和E. WHERE子句对你想要的确切标准很简单......

现在,如果您只想要某些其他元素,您只需将“AND”添加到该组件的JOIN条件即可。