简体   繁体   中英

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 ??

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. 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

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 is actually a filter to remove duplicates. So, while selecting multiple columns the DISTINCT clause should be applied to the complete set rather than a single column. 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.

DISTINCT always works on all columns, you might must put it directly after SELECT.

In MySQL there's an easy way to get only one row per img_id, add a 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.

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.

So, the:

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

is invalid SQL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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