【问题标题】:Search multiple categories and Industries from relational table using mysql query使用 mysql 查询从关系表中搜索多个类别和行业
【发布时间】:2013-09-17 09:45:06
【问题描述】:

我有三个表,我需要根据类别表中存在的搜索关键字查找行业。

供参考- 我已附上screenshot for table description

我。类别表 ID 存在于 categories_to_industries
ii. categories_to_industries id 存在于行业中
ii.每个类别都有 n 个行业。

我想要一个 mysql 查询来按类别名称获取行业名称。

希望这些信息足够了。如果还有其他关于结构的信息,请告诉我。

如果有人对此有好的解决方案,请帮助我。

【问题讨论】:

  • 请把结构和样本数据放到帖子里,让人们第一时间看到

标签: php mysql laravel


【解决方案1】:

您只需要一个简单的INNER JOIN

SELECT  a.*
FROM    Industries a
        INNER JOIN Category_to_industries b
            ON a.ID = b.industry_id
        INNER JOIN Categories c
            ON b.category_ID = c.id
WHERE   c.category_name = 'Fired'

您在 sql 查询中看到的字母(abc)称为别名。

要进一步了解有关联接的更多信息,请访问以下链接:

【讨论】:

    【解决方案2】:

    试试这个

        SELECT industry_id FROM category_to_industries WHERE category_id = 
           (
    
           SELECT id FROM categories WHERE category_name = "Fired"
           )
    

    【讨论】:

      【解决方案3】:

      这非常简单:

      SELECT Industries.* FROM Industries
      JOIN category_to_industries ON (Industries.id = category_to_industries.industry_id)
      JOIN Categories ON (Categories.id = category_to_industries.category_id)
      WHERE Categories.category_name='Fired';
      

      【讨论】:

        【解决方案4】:

        试试这个查询 -

        select industry_name from Industries where id in ( select industry_id from category_to_industries where category_id in( (select id from Categories where category_name = "Fired") ));

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-17
          • 1970-01-01
          • 1970-01-01
          • 2012-12-08
          • 1970-01-01
          相关资源
          最近更新 更多