内连接 SQL 查询(抱歉,不知道如何简洁地描述)

发布于 2024-12-03 15:13:02 字数 390 浏览 1 评论 0原文

我相信这将是一个极其简单的问题。这么简单,我会感到尴尬。无论如何,我已经尝试解决这个问题有一段时间了,所以我束手无策。以下是使用汽车制造商(即丰田)和汽车型号(即普锐斯、Sienna)的场景。

SELECT * FROM car_manufacturer man
INNER JOIN car_model cm ON cm.manufacturerid = man.manufacturerid

很简单,对吧?

现在我想为每个将一些布尔值设置为 true 的制造商返回一行(比方说 car_manufacturer.is_awesome)并且制造多个模型。

我正在尝试使用带有 select count(*) 的子查询,但无法弄清楚这一点。

请帮忙!

I believe this will be an extremely simple question. So simple that I am going to be embarrassed. Regardless, I have been trying to figure this out for a while so I'm at my wits end. Here is the scenario using car manufacturers (ie Toyota) and car models (ie Prius, Sienna).

SELECT * FROM car_manufacturer man
INNER JOIN car_model cm ON cm.manufacturerid = man.manufacturerid

Simple, right?

Now I'd like to return a row for each manufacturer that has some Boolean set to true (Lets say car_manufacturer.is_awesome) AND makes more than one model.

I'm trying to use a sub query with select count(*) but can't figure this one out.

Please help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可爱咩 2024-12-10 15:13:02
SELECT man.*
FROM   car_manufacturer man
WHERE  is_awesome = 1
       AND manufacturerid IN (SELECT manufacturerid
                             FROM   car_model
                             GROUP  BY manufacturerid
                             HAVING COUNT(DISTINCT model_name/*whatever*/) > 1)  
SELECT man.*
FROM   car_manufacturer man
WHERE  is_awesome = 1
       AND manufacturerid IN (SELECT manufacturerid
                             FROM   car_model
                             GROUP  BY manufacturerid
                             HAVING COUNT(DISTINCT model_name/*whatever*/) > 1)  
孤独岁月 2024-12-10 15:13:02

试试这个:

SELECT * 
  FROM car_manufacturer man INNER JOIN 
       (
        SELECT manufacturerid  
          FROM car_model 
         GROUP BY manufacturerid  
        HAVING COUNT(1) > 1
       ) cm 
    ON cm.manufacturerid = man.manufacturerid 
   AND man.is_awesome = 1

Try this:

SELECT * 
  FROM car_manufacturer man INNER JOIN 
       (
        SELECT manufacturerid  
          FROM car_model 
         GROUP BY manufacturerid  
        HAVING COUNT(1) > 1
       ) cm 
    ON cm.manufacturerid = man.manufacturerid 
   AND man.is_awesome = 1
过期以后 2024-12-10 15:13:02
SELECT man .manufacturerid 
From car_manufacturer man INNER JOIN car_model car  ON man.manufacturerid = car.manufacturerid 
AND is_awesome = 1
Group by (car  .manufacturerid )
HAVING COUNT(car  .manufacturerid ) > 2
SELECT man .manufacturerid 
From car_manufacturer man INNER JOIN car_model car  ON man.manufacturerid = car.manufacturerid 
AND is_awesome = 1
Group by (car  .manufacturerid )
HAVING COUNT(car  .manufacturerid ) > 2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文