php - why mysqli left join return 1 more rows? -


i have 2 tables called : 1) products 2) product_images , it's data :

products table : ============== productid   title      model_number ----------------------------------- 1           title1     123 2           title2     124 3           title3     125 4           title4     126 5           title5     127  product_images ============== pi_id    p_id   product_image ------------------------------ 1        1      image1 2        2      image2 3        3      image3 4        4      image4 5        1      image1    

so if run query it's return 5 rows, that's correct :

$q=mysqli_query($conn, "select * products"); echo $n=mysqli_num_rows($q); // return 5 rows 

but if run query it's return 6 rows, why ? it's should show 5 rows!

$searchquery =  mysqli_query($conn, "select products.productid,    products.title, products.model_number, product_images.product_image  products left join product_images on products.productid =   product_images.p_id ");  $isexist =  mysqli_num_rows($searchquery); // return 6 rows 

can tell me why it's return 1 more rows , how can solved it? thank you.

actually want show products appropriate images.

you have used left join. , have 2 records p_id=1; have 6 rows.

if want make p_id unique use group_by in query.

write query below:-

select products.productid,    products.title, products.model_number, product_images.product_image  products left join product_images on products.productid =   product_images.p_id group product_images.p_id; 

hope :)


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -