简体   繁体   中英

inner join returning duplicates

hello i have the following query:

$k=mysql_query("SELECT cat.name_url, cat.id 
                    FROM category cat  
                    WHERE cat.name_url='".$_GET["category"]."' ");
 $kRow=mysql_fetch_assoc($k);


$j=mysql_query("SELECT s_cat.name, s_cat.name_url, s_cat.category_id 
                    FROM s_category s_cat 
                        INNER JOIN category cat 
                            ON s_cat.category_id=".$kRow["id"]." 
                    ORDER BY s_cat.name ASC ") or die (mysql_error());

i first fetch a $_GET then use it in my second query but there i get duplicated rows when i fetch the arrays , if instead of using INNER JOIN i build my second query as:

$j=mysql_query("SELECT s_cat.name, s_cat.name_url, s_cat.category_id 
                    FROM s_category s_cat 
                    WHERE s_cat.category_id=".$kRow["id"]." 
                    ORDER BY s_cat.name ASC ") or die (mysql_error());

it will work and I wont get duplicates in the fetch row, waht is wrong with my INNER JOIN query?

cheers

syntax for inner join:

SELECT a.name, b.text 
FROM a INNER JOIN b ON a.id = b.a_id
WHERE a.name = 'something'

you're missing the "ON a.smth=b.smth" part

使用SELECT DISTINCT,它将删除第一个查询中的重复项

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