简体   繁体   English

DISTINCT with as子句

[英]DISTINCT with as clause

$query="SELECT a.pk_i_id,a.i_price,b.s_title,c.pk_i_id AS img_id,c.s_extension,d.s_city,d.s_city_area from zl_t_item a, zl_t_item_description b, zl_t_item_resource c, zl_t_item_location d where a.fk_i_category_id=$cat_id and a.pk_i_id=b.fk_i_item_id and a.pk_i_id=c.fk_i_item_id and a.pk_i_id=d.fk_i_item_id ORDER BY a.dt_pub_date DESC";

In this above query i need to add DISTINCT before this c.pk_i_id AS img_id ?? 在上述查询中,我需要在此c.pk_i_id AS img_id之前添加DISTINCT?

it shows error when i did like below 当我像下面一样显示错误

$query="SELECT a.pk_i_id,a.i_price,b.s_title,DISTINCT c.pk_i_id AS img_id,c.s_extension,d.s_city,d.s_city_area from zl_t_item a, zl_t_item_description b, zl_t_item_resource c, zl_t_item_location d where a.fk_i_category_id=$cat_id and a.pk_i_id=b.fk_i_item_id and a.pk_i_id=c.fk_i_item_id and a.pk_i_id=d.fk_i_item_id ORDER BY a.dt_pub_date DESC";

what is the problem on it?. 这是什么问题?

It is invalid use of DISTINCT keyword. 无效使用DISTINCT关键字。 You can only apply it on a set of columns and not for a specific column skipping other columns 您只能将其应用于一组列,而不能应用于跳过其他列的特定列

DISTINCT should be applied right after SELECT for a column or set of columns you cannot use DISTINCT between the columns DISTINCT应之后被应用于SELECT列或列集不能使用DISTINCT列之间

SELECT  DISTINCT c.pk_i_id AS img_id, 
a.pk_i_id,a.i_price,b.s_title,c.s_extension,d.s_city,d.s_city_area 
from zl_t_item a, zl_t_item_description b, zl_t_item_resource c,
 zl_t_item_location d where a.fk_i_category_id=$cat_id 
and a.pk_i_id=b.fk_i_item_id and a.pk_i_id=c.fk_i_item_id 
and a.pk_i_id=d.fk_i_item_id ORDER BY a.dt_pub_date DESC

In general, using DISTINCT is performance kill. 通常,使用DISTINCT会导致性能下降。 DISTINCT is actually a filter to remove duplicates. DISTINCT实际上是用于删除重复项的过滤器。 So, while selecting multiple columns the DISTINCT clause should be applied to the complete set rather than a single column. 因此,在选择多个列时,应将DISTINCT子句应用于完整集而不是单个列。 Hence you are seeing an error. 因此,您看到一个错误。

The query can be rewritten based on the requirements. 可以根据需求重写查询。 If you want filter out duplicates then either you can apply row rank, or group by and having clause to achieve the intended results. 如果要过滤掉重复项,则可以应用行排名,也可以使用by和having子句来实现预期的结果。

DISTINCT always works on all columns, you might must put it directly after SELECT. DISTINCT始终适用于所有列,您可能必须将其直接放在SELECT之后。

In MySQL there's an easy way to get only one row per img_id, add a GROUP BY img_id 在MySQL中,有一种简单的方法可以使每个img_id仅获得一行,并添加一个GROUP BY img_id

SELECT
  a.pk_i_id
  ,a.i_price
  ,b.s_title
  ,c.pk_i_id AS img_id
  ,c.s_extension
  ,d.s_city
  ,d.s_city_area
from
  zl_t_item a
  ,zl_t_item_description b
  ,zl_t_item_resource c
  ,zl_t_item_location d
where
  a.fk_i_category_id = $cat_id
  and a.pk_i_id = b.fk_i_item_id
  and a.pk_i_id = c.fk_i_item_id
  and a.pk_i_id = d.fk_i_item_id
GROUP BY img_id
ORDER BY
  a.dt_pub_date DESC

Of course this is a proprietary MySQL syntax which breaks all the rules of relational dabatabses and will not work with any other RDBMS. 当然,这是一种专有的MySQL语法,它破坏了关系dabatabses的所有规则,不适用于任何其他RDBMS。

You can have either SELECT DISTINCT <columns> or SELECT <columns> (which actually defaults to SELECT ALL <columns> .) You can't apply DISTINCT to a specific column. 您可以SELECT DISTINCT <columns>SELECT <columns> (实际上默认为SELECT ALL <columns> 。)。您不能将DISTINCT应用于特定列。

So, the: 所以:

SELECT a.pk_i_id ,a.i_price, b.s_title, DISTINCT c.pk_i_id ...

is invalid SQL. 是无效的SQL。

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

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