如何:MySQL如何根据三个属性检索记录?

发布于 2024-07-14 01:34:52 字数 641 浏览 3 评论 0原文

我是 SQL 和关系 DBMS 的新手。 我想从关系数据库中检索满足 3 个给定属性的记录。

例如,如果有一个包含有关固体信息的表:

table_solid : |s_id|s_name|description|
table_width : |w_id|w_name|
table_height: |h_id|h_name|
table_length: |l_id|l_name|

其中 *_id 都是主键。

我还有 3 个表用于将这些表与外键相关联。

table_s_h: |s_id|h_id|
table_s_w: |s_id|w_id|
table_s_l: |s_id|l_id|

请您解释一下我如何加入这些。

提前致谢。

                 --------- table_length
table_SOLID ----
                ----------- table_width

                ------------table_height

table_length 包含solid 可以采用的有效长度(与其他表类似)。

I am new to SQL and relational DBMS.
I want to retrieve records from a relational database if they satisfy 3 given properties.

For example, if there is a table containing info about solids:

table_solid : |s_id|s_name|description|
table_width : |w_id|w_name|
table_height: |h_id|h_name|
table_length: |l_id|l_name|

where *_id are all primary keys.

I have 3 more tables for relating these tables with Foreign Keys.

table_s_h: |s_id|h_id|
table_s_w: |s_id|w_id|
table_s_l: |s_id|l_id|

Would you please explain how do I join these.

Thanks in advance.

                 --------- table_length
table_SOLID ----
                ----------- table_width

                ------------table_height

table_length contains valid lengths that solid can take (and similarly with other tables).

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

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

发布评论

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

评论(3

像极了他 2024-07-21 01:34:52

来自单个表:

Select * 
FROM TABLE_NAME
WHERE table_width = SOME_VALUE3
AND table_height= SOME_VALUE2
AND table_length = SOME_VALUE3

这是您正在寻找的吗? 或者你想查询多个表? 如果是这样,请尝试以下操作:

Select * 
FROM TABLE_SOLID solid
Inner join table_width width on solid.w_id = width.w_id
inner join table_height height on solid.h_id = height.h_id
inner join table_length length on solid.l_id = length.l_id

此链接可能对您有用 http:// /dev.mysql.com/doc/refman/5.0/en/join.html

From a single table:

Select * 
FROM TABLE_NAME
WHERE table_width = SOME_VALUE3
AND table_height= SOME_VALUE2
AND table_length = SOME_VALUE3

Is that what you are looking for? Or are you trying to query multiple tables? If so try this:

Select * 
FROM TABLE_SOLID solid
Inner join table_width width on solid.w_id = width.w_id
inner join table_height height on solid.h_id = height.h_id
inner join table_length length on solid.l_id = length.l_id

This link may be of use to you http://dev.mysql.com/doc/refman/5.0/en/join.html

橘虞初梦 2024-07-21 01:34:52

我不清楚你的数据库架构。

这四个表是不同的吗? 如果是,它们是如何链接的,即如何检索给定实体的宽度、高度、长度?

或者这四列在 1 个表中,由 s_id 标识?

请澄清。

Your DB schema is not clear to me.

Are these four different tables? If yes, how are they linked up, ie how do you retrieve width, height, length for a given solid?

Or are those four columns in 1 table, identified by s_id?

Please clarify.

银河中√捞星星 2024-07-21 01:34:52

我希望我已经理解了你的架构。

SELECT
    s.*
FROM
    table_solid AS s
WHERE
    s.s_id IN
(
(SELECT s_id FROM table_s_h INNER JOIN table_height USING (h_id) WHERE h_name = H)
INTERSECT
(SELECT s_id FROM table_s_w INNER JOIN table_width USING (w_id) WHERE w_name = W)
INTERSECT
(SELECT s_id FROM table_s_l INNER JOIN table_length USING (l_id) WHERE l_name = L)
);

OT:我不知道这是否适用于 MySQL,而且我不在乎 - 我已在问题中添加了“mysql”标签,而您已将其删除。

I hope I've understood your schema.

SELECT
    s.*
FROM
    table_solid AS s
WHERE
    s.s_id IN
(
(SELECT s_id FROM table_s_h INNER JOIN table_height USING (h_id) WHERE h_name = H)
INTERSECT
(SELECT s_id FROM table_s_w INNER JOIN table_width USING (w_id) WHERE w_name = W)
INTERSECT
(SELECT s_id FROM table_s_l INNER JOIN table_length USING (l_id) WHERE l_name = L)
);

OT: I don't know if that will work in MySQL and I don't care - I've added "mysql" tag to the question and you've removed it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文