[英]MySQL get rows but prefer one column value over another
A bit of a strange one, I want to write a MySQL query that will get results from a table, but prefer one value of a column over another, ie 有点奇怪,我想编写一个MySQL查询,该查询将从表中获取结果,但更喜欢列的一个值而不是另一个
id name value prioirty
1 name1 value1 NULL
2 name1 value1 1
3 name2 value2 NULL
4 name3 value3 NULL
So here name1 has two entries, but one has a prioirty of 1. I want to get all the values from the table, but prefer the values with whatever priorty I'm after. 因此,这里的name1有两个条目,但其中一个的优先级为1。我想从表中获取所有值,但是更喜欢优先考虑的值。
The results I'd be after would be 我想要的结果是
id name value prioirty
2 name1 value1 1
3 name2 value2 NULL
4 name3 value3 NULL
An equivalent way of saying it would be 'get all rows from the table, but prefer rows with a priority of x'. 一种等效的说法是“从表中获取所有行,但优先级为x的行”。
This should do it: 应该这样做:
SELECT
T1.id,
T1.name,
T1.value,
T1.priority
FROM
My_Table T1
LEFT OUTER JOIN My_Table T2 ON
T2.name = T1.name AND
T2.priority > COALESCE(T1.priority, -1)
WHERE
T2.id IS NULL
This also allows you to have multiple priority levels with the highest being the one that you want to return (if you had a 1 and 2, the 2 would be returned). 这还允许您具有多个优先级,最高优先级是您要返回的优先级(如果您有1和2,则将返回2)。
I will also say though that it does seem like there are some design problems in the DB. 我还要说,虽然看起来数据库中确实存在一些设计问题。 My approach would have been:
我的方法是:
My_Table (id, name) My_Values (id, priority, value) with an FK on id to id. My_Table(ID,名称)My_Values(ID,优先级,值),ID到ID上带有FK。 PKs on id in My_Table and id, priority in My_Values.
My_Table中的id和My_Values中的id的PK。 Of course, I'd use appropriate table names too.
当然,我也会使用适当的表名。
You need to redesign your table first. 您需要首先重新设计表格。
It should be: 它应该是:
YourTable (Id, Name, Value)
YourTablePriority (PriorityId, Priority, Id)
Update: 更新:
select * from YourTable a
where a.Id not in
(select b.Id from YourTablePriority b)
This should work in sql server, you may need a little change to make it work in mysql. 这应该可以在sql server中工作,您可能需要做一些改动才能使其在mysql中工作。
Maybe something like: 也许像这样:
SELECT id, name, value, priority FROM
table_name GROUP BY name ORDER BY priority
Although not having a database in front of me I can't test it... 虽然我面前没有数据库,但我无法测试...
If I understand correctly, you want the value
of a name
given a specific priority
, or the value
associated with a NULL priority
. 如果我理解正确的话,你想要的
value
一的name
赋予了特定的priority
,或value
与NULL相关的priority
。 (You do not necessarily want the MAX(priority)
that exists.) (您不必一定要存在
MAX(priority)
。)
Yes, you've got some awkward design issues which you should address, but let's solve the problem you do have at present (and you can later migrate to the problem you ought to have :) ): 是的,您遇到了一些尴尬的设计问题,应该解决,但是让我们解决您目前遇到的问题(以后您可以迁移到您应该遇到的问题:)):
mysql> SET @priority = 1; -- the priority we want, if recorded
mysql> PREPARE stmt FROM "
SELECT
t0.*
FROM
t t0
LEFT JOIN
(SELECT DISTINCT name, priority FROM t WHERE priority = ?) t1
ON t0.name = t1.name
WHERE
t0.priority = t1.priority
OR
t1.priority IS NULL
";
mysql> EXECUTE stmt USING @priority;
+----+-------+--------+----------+
| id | name | value | priority |
+----+-------+--------+----------+
| 2 | name1 | valueX | 1 |
| 3 | name2 | value2 | NULL |
| 4 | name3 | value3 | NULL |
+----+-------+--------+----------+
3 rows in set (0.00 sec)
(Note that I changed the prioritized value
of "name1" to "valueX" in the above -- your original formulation had identical value
values for "name1" regardless of priority, which made it hard for me to understand why you cared to discriminate one from the other.) (请注意,我改变了优先
value
在上面的“名1”到“valueX”的-你原来的配方具有相同的value
“NAME1”的,无论优先级,这使我很难理解为什么值照顾你辨别一个从另一个。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.