简体   繁体   English

在SQL查询中获取唯一数据

[英]Get a unique data in a SQL query

I've a database who contain some datas in that form: 我有一个数据库,其中包含该格式的一些数据:

icon(name, size, tag)
(myicon.png, 16, 'twitter')
(myicon.png, 32, 'twitter')
(myicon.png, 128, 'twitter')
(myicon.png, 256, 'twitter')
(anothericon.png, 32, 'facebook')
(anothericon.png, 128, 'facebook')
(anothericon.png, 256, 'facebook')

So as you see it, the name field is not uniq I can have multiple icons with the same name and they are separated with the size field. 因此,如您所见,名称字段不是唯一的,我可以有多个具有相同名称的图标,并且它们由大小字段分隔。 Now in PHP I have a query that get ONE icon set, for example : 现在在PHP中,我有一个获得一个图标集的查询,例如:

$dbQueryIcons = mysql_query("SELECT * FROM pl_icon WHERE tag LIKE '%".$SEARCH_QUERY."%' GROUP BY name ORDER BY id DESC LIMIT ".$firstEntry.", ".$CONFIG['icon_per_page']."") or die(mysql_error());

With this example if $tag contain 'twitter' it will show ONLY the first SQL data entry with the tag 'twitter', so it will be : 在此示例中,如果$ tag包含'twitter',它将仅显示带有标签'twitter'的第一个SQL数据条目,因此它将是:

(myicon.png, 16, 'twitter')

This is what I want, but I would prefer the '128' size by default. 这就是我想要的,但是默认情况下,我希望使用'128'大小。 Is this possible to tell SQL to send me only the 128 size when existing and if not another size ? 是否可以告诉SQL仅向我发送128大小(如果没有)给我?

In an another question someone give me a solution with the GROUP BY but in this case that don't run because we have a GROUP BY name. 在另一个问题中,有人给了我GROUP BY的解决方案,但由于我们有GROUP BY名称,因此无法运行。 And if I delete the GROUP BY, it show me all size of the same icons. 而且,如果我删除GROUP BY,则会显示相同图标的所有大小。

Thanks ! 谢谢 !

Try using subqueries. 尝试使用子查询。

SELECT * FROM (

    SELECT * FROM pl_icon
    WHERE tag LIKE '%$SEARCH_QUERY%'
    ORDER BY IF(size = 128, 0, 1)

) pl_icon
GROUP BY name
ORDER BY id DESC
LIMIT ...

We find all matching rows in the inner query and place rows with size = 128 above all others. 我们在内部查询中找到所有匹配的行,并将大小= 128的行置于所有其他行之上。 Then, for each unique name we choose only the first record using GROUP BY . 然后,对于每个唯一名称,我们仅使用GROUP BY选择第一条记录。

尝试以下ORDER BY子句(id或size?):

ORDER BY case when size=128 then 999999 else size end DESC

If what you really want is get the record with the largest size then: 如果您真正想要的是获得最大尺寸的记录,则:

SELECT name, tag, MAX(size)
FROM pl_icon 
WHERE tag LIKE '%".$SEARCH_QUERY."%' 
GROUP BY name, tag

But what's confusing here is that the 'name' is not unique. 但是,这里令人困惑的是,“名称”不是唯一的。 Usually in such scenarios people want to identify the record where a maximum (or minimum) occurs - like the last meter reading, or biggest invoice. 通常,在这种情况下,人们希望识别出现最大(或最小)值的记录-例如最近的读表或最大的发票。 So if your table had an id field and you wanted to retrieve that: 因此,如果表中有一个id字段,而您想检索该字段:

SELECT pl2.*
FROM pl_icon pl2,
(SELECT name, tag, MAX(size) AS biggest
FROM pl_icon 
WHERE tag LIKE '%".$SEARCH_QUERY."%' 
GROUP BY name, tag) ilv
WHERE ilv.name=pl2.name
AND ilv.tag=pl2.tag
AND ilv.biggest=pl2.size;

However this can be rather inefficient for very large tables / selects - when you might want to try the MAX(CONCAT trick 但是,这对于非常大的表/选择来说可能效率不高-当您可能想尝试MAX(CONCAT技巧)

C. C。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM