SQL:从连接中收集右手值
假设一个问题通过称为标记的连接表有许多标签。我这样进行连接:
SELECT DISTINCT `questions`.id
FROM `questions`
LEFT OUTER JOIN `taggings`
ON `taggings`.taggable_id = `questions`.id
LEFT OUTER JOIN `tags`
ON `tags`.id = `taggings`.tag_id
我想根据特定的标签名称(例如“钢琴”)对结果进行排序,以便钢琴位于顶部,然后按字母顺序排列所有其他标签。目前我正在使用这个订单条款:
ORDER BY (tags.name = 'piano') desc, tags.name
这是完全错误的 - 我得到的第一个结果甚至根本没有标记“钢琴”。我认为我的问题是,我需要以某种方式对标签名称进行分组,并针对它进行排序测试:我认为,由于结果连接表的结构,针对直接的tags.name 进行排序是行不通的(它确实有效)如果我只是在标签表上进行简单的选择)但我不知道如何修复它。
感谢您的任何建议,最大
编辑-回复马塞洛重新合并 非常感谢马塞洛 - 我以前没见过这个。必须更正确地阅读api。 这确实有帮助,但前提是我也选择了合并子句。即,这:
SELECT DISTINCT `questions`.id
FROM `questions`
LEFT OUTER JOIN `taggings`
ON `taggings`.taggable_id = `questions`.id
LEFT OUTER JOIN `tags`
ON `tags`.id = `taggings`.tag_id
ORDER BY (COALESCE(tags.name,'') = 'piano') desc, tags.name
仍然给出虚假结果。然而,这:
SELECT DISTINCT `questions`.id, COALESCE(tags.name,'')
FROM `questions`
LEFT OUTER JOIN `taggings`
ON `taggings`.taggable_id = `questions`.id
LEFT OUTER JOIN `tags`
ON `tags`.id = `taggings`.tag_id
ORDER BY (COALESCE(tags.name,'') = 'piano') desc, tags.name
返回正确的结果。但我仍然想选择问题 ID。无论如何,肯定会越来越接近......
Let's say a question has many tags, via a join table called taggings. I do a join thus:
SELECT DISTINCT `questions`.id
FROM `questions`
LEFT OUTER JOIN `taggings`
ON `taggings`.taggable_id = `questions`.id
LEFT OUTER JOIN `tags`
ON `tags`.id = `taggings`.tag_id
I want to order the results according to a particular tag name, eg 'piano', so that piano is at the top, then by all the other tags in alphabetical order. Currently i'm using this order clause:
ORDER BY (tags.name = 'piano') desc, tags.name
Which is going completely wrong - the first results i get back aren't even tagged with 'piano' at all. I think my problem is that i need to group the tag names somehow and do my ordering test against that: i think that doing it against the straight tags.name isn't working due to the structure of the resultant join table (it does work if i just do a simple select on the tags table) but i can't get my head around how to fix it.
grateful for any advice, max
EDIT - reply to Marcelo re COALESCE
Thanks a lot Marcelo - i hadn't seen this before. Must read api's more properly.
This does actually help, but only if i select the coalese clause as well. Ie, this:
SELECT DISTINCT `questions`.id
FROM `questions`
LEFT OUTER JOIN `taggings`
ON `taggings`.taggable_id = `questions`.id
LEFT OUTER JOIN `tags`
ON `tags`.id = `taggings`.tag_id
ORDER BY (COALESCE(tags.name,'') = 'piano') desc, tags.name
still gives spurious results. However, this:
SELECT DISTINCT `questions`.id, COALESCE(tags.name,'')
FROM `questions`
LEFT OUTER JOIN `taggings`
ON `taggings`.taggable_id = `questions`.id
LEFT OUTER JOIN `tags`
ON `tags`.id = `taggings`.tag_id
ORDER BY (COALESCE(tags.name,'') = 'piano') desc, tags.name
returns the correct results. I'd like to still just select the question ids though. Definitely getting closer anyway...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许是因为当
tags.name
为NULL
时,tags.name = 'piano'
的计算结果为NULL
。尝试COALESCE(tags.name, '') = 'piano'
。Maybe because
tags.name = 'piano'
evaluates toNULL
whentags.name
isNULL
. TryCOALESCE(tags.name, '') = 'piano'
.