MySQL 查询建议请求

发布于 2024-12-09 09:16:41 字数 856 浏览 0 评论 0原文

我可以就这个查询获得一些帮助吗?我会详细解释。为了方便起见,我将举一个包含 HTML 标签和属性的示例。

我有 3 个表:

  • tbl1 - 包含所有 HTML 标记(tagId, tagName
  • tbl2 - 包含可能出现在 Tags (attId, attName)
  • tbl3 - 是 tbl1 和 tbl2 的映射表 (tagId, attId)

I想要选择属于所选标签(在下面的示例中标签 ID 4)和标签 ID 的所有属性(以及有关属性的相关信息)。

以下是我希望从查询中获得的内容的示例:

attId  tagId   attName  
50     4       The name of the attribute with id 50
89     4       The name of the attribute with id 89
114    4       The name of the attribute with id 114

下面是我所做的查询,但我相信有更好的方法。

SELECT tbl2.*, tbl3.tagId
FROM tbl2 JOIN tbl3 
WHERE tbl3.attId IN (
     SELECT tbl3.attId FROM tbl3 where tbl3.tagId=4
     )
AND tbl3.attIdd = tbl2.attId
GROUP BY tbl3.attId

提前致谢。

Can I get some help with this query. I will explain in details. To make it easier I'll take an example with HTML tags and attributes.

I have 3 table:

  • tbl1 - contains all the HTML tags (tagId, tagName)
  • tbl2 - contains all the available attributes that might appear in the
    tags (attId, attName)
  • tbl3 - is a map table for tbl1 and tbl2 (tagId, attId)

I want to select all the attributes (and related information about the attributes) that belong to the chosen tag (in the example below tag id 4) and the ID of the tag.

Here is an example of what I'd like to get from the query:

attId  tagId   attName  
50     4       The name of the attribute with id 50
89     4       The name of the attribute with id 89
114    4       The name of the attribute with id 114

Below is the query that I've made, but I believe there is a better way.

SELECT tbl2.*, tbl3.tagId
FROM tbl2 JOIN tbl3 
WHERE tbl3.attId IN (
     SELECT tbl3.attId FROM tbl3 where tbl3.tagId=4
     )
AND tbl3.attIdd = tbl2.attId
GROUP BY tbl3.attId

Thanks in advance.

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

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

发布评论

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

评论(3

笨笨の傻瓜 2024-12-16 09:16:41

问题 1
您正在执行交叉联接,稍后通过在 where 子句中放置过滤器将其简化为内部联接。
这是使用隐式连接语法时的旧方法。
在大多数 SQL 方言(但不是 MySQL)上,这会产生错误。
切勿在没有 ON 子句的情况下进行连接
如果要进行交叉联接,请使用以下语法:select * from a cross join b

问题 2
sub select 确实不需要,您可以在 where 子句中进行测试。

SELECT tbl2.*, tbl3.tagId 
FROM tbl2 
INNER JOIN tbl3 ON (tbl3.attIdd = tbl2.attId)
WHERE tbl3.tagId = 4 
GROUP BY tbl3.attId 

问题 3a
您正在 tbl3.ATTid 上进行分组,在这种情况下,这与在 tbl2.attid 上进行分组相同(因为>ON (tbl3.attIdd = tbl2.attId) 子句)
后者更有意义,因为您正在执行 select tbl2.*,而不是 select tbl3.*

Issue 3b
如果 attId 不是 tbl2 的主键或唯一键,则查询结果将是不确定的。
这意味着查询将从具有相同 attID 的可能行列表中随机选择一行。如果您不希望这样做,则必须包含一个 having 子句,该子句将根据某些条件选择一行。

示例

/*selects the most recent row per attID, instead of a random one*/
SELECT tbl2.*, tbl3.tagId 
FROM tbl2 
INNER JOIN tbl3 ON (tbl3.attIdd = tbl2.attId)
WHERE tbl3.tagId = 4 
GROUP BY tbl2.attId 
HAVING tbl2.dateadded = MAX(tabl2.dateadded) 

Issue 1
You're doing cross join that you later reduce to an inner join by putting a filter in the where clause.
This is the old-skool way when using implicit join syntax.
On most SQL-dialects (but not MySQL) this gives an error.
Never do a join without a ON clause.
If you want to do a cross join, use this syntax: select * from a cross join b

Issue 2
The sub select is really not needed, you can just do the test inside a where clause.

SELECT tbl2.*, tbl3.tagId 
FROM tbl2 
INNER JOIN tbl3 ON (tbl3.attIdd = tbl2.attId)
WHERE tbl3.tagId = 4 
GROUP BY tbl3.attId 

Issue 3a
You are doing a group by on tbl3.ATTid, which in this context is the same as doing a group by on tbl2.attid (because of the ON (tbl3.attIdd = tbl2.attId) clause)
The latter makes a bit more sense, because you are doing select tbl2.*, not select tbl3.*

Issue 3b
If attId is not a primary or unique key for tbl2, than the result of the query will be indeterminate.
That means that the query will select a row at random from a list of possible rows with the same attID. If you don't want that you'll have to include a having clause that will choose a row based on some criterion.

Example

/*selects the most recent row per attID, instead of a random one*/
SELECT tbl2.*, tbl3.tagId 
FROM tbl2 
INNER JOIN tbl3 ON (tbl3.attIdd = tbl2.attId)
WHERE tbl3.tagId = 4 
GROUP BY tbl2.attId 
HAVING tbl2.dateadded = MAX(tabl2.dateadded) 
只为一人 2024-12-16 09:16:41

你可以这样做。

SELECT tbl2.*, tbl3.tagId
FROM tbl2 JOIN tbl3 
ON tbl3.attId = tbl2.attId
WHERE tbl2.tagId = <selected id>
GROUP BY tbl3.attId

You can do it like this instead.

SELECT tbl2.*, tbl3.tagId
FROM tbl2 JOIN tbl3 
ON tbl3.attId = tbl2.attId
WHERE tbl2.tagId = <selected id>
GROUP BY tbl3.attId
平安喜乐 2024-12-16 09:16:41
SELECT  tbl2.attId, tbl1.tagId, tbl2.attName FROM tbl3, tbl1, tbl2 
   WHERE tbl1.tagId = tbl3.tagId AND tbl2.attId = tbl3.attId AND tbl3.tagId = 4
SELECT  tbl2.attId, tbl1.tagId, tbl2.attName FROM tbl3, tbl1, tbl2 
   WHERE tbl1.tagId = tbl3.tagId AND tbl2.attId = tbl3.attId AND tbl3.tagId = 4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文