MySQL 连接两个表中的 LIMIT 1

发布于 2024-08-12 21:30:54 字数 600 浏览 5 评论 0原文

我有两张桌子。一份包含有关属性的信息。另一个为每个属性存储 3 个图像。在这三幅图像中,一幅被标记为“主”图像。

所以我有:

Properties:
p_id    name
1   villa a
2   villa b
3   villa c
4   villa d

并且

    Images
i_id p_id main
1    1     0
2    1     0
3    1     1
4    2     0
5    2     1
6    2     0

我需要生成一个查询,该查询返回所有属性及其主图像的 id。例如,

p_id  name    i_id
1     villa a  3
2     villa b  5

我知道这将涉及使用 LIMIT 1 和连接,但不知道从哪里开始,我已经尝试通过使用子查询来做到这一点,但觉得它一定比我正在做的复杂......

*我该怎么做* 使其通过“main”选择前 1 个来对查询进行排序(即,如果未设置 main,它仍会选择图像)?

I have two tables. One with information about properties. The other stores 3 images for each property. Of these three images - one is marked as being the "main" image.

So I have:

Properties:
p_id    name
1   villa a
2   villa b
3   villa c
4   villa d

and

    Images
i_id p_id main
1    1     0
2    1     0
3    1     1
4    2     0
5    2     1
6    2     0

I need to produce a query which returns all of the properties with the id of their main image. e.g.

p_id  name    i_id
1     villa a  3
2     villa b  5

I know this will involve using LIMIT 1 and a join, but not sure where to start, I have already attempted doing this by using a subquery but felt it must be less complicated than what I was doing....

* HOW DO I *
Make it so it orders the query by "main" selecting the top 1 (i.e. so if main is not set it will still select an image) ?

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

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

发布评论

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

评论(5

情痴 2024-08-19 21:30:54

这是一种方法:

select      *
from        properties p
left join   images i 
on          p.p_id = i.p_id 
and         i.main = 1

如果未找到主图像,左连接将返回 NULL 图像。

Here's one way:

select      *
from        properties p
left join   images i 
on          p.p_id = i.p_id 
and         i.main = 1

The left join will return a NULL image if no main image is found.

伪装你 2024-08-19 21:30:54

给你:

SELECT p_id, name, i_id
FROM properties p INNER JOIN images i ON (p.p_id = i.p_id AND i.main = 1)

或者

SELECT p_id, name, i_id
FROM properties p INNER JOIN images i ON (p.p_id = i.p_id)
WHERE i.main = 1

Here you go:

SELECT p_id, name, i_id
FROM properties p INNER JOIN images i ON (p.p_id = i.p_id AND i.main = 1)

or

SELECT p_id, name, i_id
FROM properties p INNER JOIN images i ON (p.p_id = i.p_id)
WHERE i.main = 1
写下不归期 2024-08-19 21:30:54
SELECT p.p_id, p.name i.i_id FROM properties p JOIN images i USING p_id where p_id = 1;
SELECT p.p_id, p.name i.i_id FROM properties p JOIN images i USING p_id where p_id = 1;
夕嗳→ 2024-08-19 21:30:54
select p.p_id, p.name, i.i_id 
from properties p join images i on p.p_id = i.p_id 
where i.main=1
select p.p_id, p.name, i.i_id 
from properties p join images i on p.p_id = i.p_id 
where i.main=1
放飞的风筝 2024-08-19 21:30:54

您想要使用如下查询:

SELECT p.p_id, p.name, i.i_id FROM Images i INNER JOIN Properties p ON p.p_id = i.p_id WHERE i.main = 1

You want to use a query like the following:

SELECT p.p_id, p.name, i.i_id FROM Images i INNER JOIN Properties p ON p.p_id = i.p_id WHERE i.main = 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文