Search only the data of two tables with different id's

1

Good,

I have 2 tables:

Cars:

  • id;
  • template;
  • comb;
  • price;

Car_img:

  • id;
  • id_fk; (foreign key of Cars)
  • img;

I do this research:

SELECT DISTINCT carros.id, carros.modelo, carros_img.img, carros_desc.comb, carros_desc.preco 
	from carros, carros_desc, carros_img 
	where carros.id = carros_desc.id_fk 
	AND carros.id = carros_img.id_fk 
	order by carros.id ASC

And it appears:

Noid(idofthecarstable)appearssomeid'ssameasIdotoonlyappear1ofeachtype:

    
asked by anonymous 16.09.2016 / 11:32

2 answers

3

I think GROUP BY resolves (not tested)

SELECT DISTINCT carros.id, carros.modelo, carros_img.img, carros_desc.comb, carros_desc.preco 
from carros, carros_desc, carros_img 
where carros.id = carros_desc.id_fk 
AND carros.id = carros_img.id_fk 
GROUP BY carros.id 
order by carros.id ASC
    
16.09.2016 / 12:04
1

In the Mimes_Filters table you should have several lines for the same id but with the different img field.

Your query is correct but to appear only 1 line of each type, you have to make sure that the img column is not in the select or else you are obliged to display only one type of image.

Example:

SELECT DISTINCT carros.id, carros.modelo, carros_img.img, carros_desc.comb, carros_desc.preco 
    from carros, carros_desc, carros_img 
    where carros.id = carros_desc.id_fk 
    AND carros.id = carros_img.id_fk    
        AND carros_img.img = 'smart2.jpg' -- só smart2 
order by carros.id ASC
    
16.09.2016 / 12:13