Jump to content

plz help in query for distinct field


delickate

Recommended Posts

hi, i'm accessing data from 4 tables and want to filter single field that is repeating.

i want to not show repeatition.my query is........

SELECT DISTINCT (

cl.lst_code

), c.cat_id, c.cat_name, ct.ct_id, ct.ct_typename, cl.lst_id, cl.lst_name, cl.lst_description, cl.lst_largeimage, cl.lst_smallimage, cl.cat_id, cl.ct_id, cd.cd_id, cd.cd_code, cd.cd_description, cd.cd_largeimage, cd.cd_smallimage, cd.lst_id

FROM tbl_category c, tbl_categorytype ct, tbl_categorylist cl, tbl_categorydetail cd

WHERE cl.cat_id = c.cat_id

AND cl.ct_id = ct.ct_id

AND cd.lst_id = cl.lst_id

 

here cl.lst_code is the field dat i want to filter.

by this query it show repeatition of this field.

can any one help me?

Link to comment
https://forums.phpfreaks.com/topic/104518-plz-help-in-query-for-distinct-field/
Share on other sites

$sql = "SELECT DISTINCT (
cl.lst_code
), c.cat_id, c.cat_name, ct.ct_id, ct.ct_typename, cl.lst_id, cl.lst_name, cl.lst_description, cl.lst_largeimage, cl.lst_smallimage, cl.cat_id, cl.ct_id, cd.cd_id, cd.cd_code, cd.cd_description, cd.cd_largeimage, cd.cd_smallimage, cd.lst_id
FROM tbl_category c, tbl_categorytype ct, tbl_categorylist cl, tbl_categorydetail cd
WHERE cl.cat_id = c.cat_id
AND cl.ct_id = ct.ct_id
AND cd.lst_id = cl.lst_id";
$result = ($sql,$conn);
//
//declare empty variable
//
$lst_code = "";
while ($array = mysql_fetch_array($result)) {
//
// after $lst_code has been set, it wont display it again
// 
if ($lst_code <> $array[cl.lst_code]) 
$lst_code = $array[cl.lst_code];
echo "$lst_code";
}
}

Not sure if thats what your looking for, but I did something like that when looping through categories in a table. I'm almost positive there is a better way.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.