Select from all companies that have catalogs

1

I'm having a problem with a select in two tables that are returning the value twice. I have the companies table and table catalogs that are linked by company.id and catalogs.id_enterprise . When I make a select that has two or more catalogs from the same company, the company appears more than once.

CREATE TABLE IF NOT EXISTS 'tbl_empresa' (
  'id' int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY ('id')
)

CREATE TABLE IF NOT EXISTS 'tbl_catalogos' (
  'id' int(10) unsigned NOT NULL AUTO_INCREMENT,
  'tbl_empresa_id' int(11) NOT NULL,
  PRIMARY KEY ('id'),
  KEY 'tbl_empresa_id' ('tbl_empresa_id')
) 

I need to select all companies that have catalogs. How do I do that? What would the query look like?

    
asked by anonymous 18.05.2016 / 15:29

2 answers

1

To check companies who have catalog you need to join with catalogs, but since you do not need to display catalogs only companies , you have two alternatives: select only companies with distinct or select only companies using group by in it.

With distinct :

select distinct e.* from tbl_empresa e 
join tbl_catalogos c on (e.id = c.tbl_empresa_id);

With group by :

select e.* from tbl_empresa e 
join tbl_catalogos c on (e.id = c.tbl_empresa_id)
group by e.id;
    
18.05.2016 / 15:40
0
SELECT e.id
FROM tbl_empresa AS e
INNER JOIN tbl_catalogo AS c
ON e.id = c.tbl_empresa_id
    
18.05.2016 / 15:41