简体   繁体   中英

Query to select from multiple tables

I know that there are several posts about this, but I can't get this to work. I don't have any experience of MySQL "join" or "left", only simple querys.

I've got 3 tables: categories, companies and catcomp

Categories

id | name | ... | ... |

1 | Foo

2 | Bar

Companies

id | name | ... | ...

1 | Company1

2 | Company2

Catcomp (To store multiple categories for one company)

company_id | category_id

1 | 1

1 | 2

2 | 2

I've only got this:

$result = mysql_query("SELECT * FROM categories");
while($row = mysql_fetch_array($result))
{
    echo '<input type="checkbox" id="'.$row['id'].'" name="cat[]" value="'.$row['id'].'">'.$row['name'].'<br>
}

This prints out all categories with check boxes.. I want the boxes to be checked for the current company.

Any ideas?

Select c.category_id,c.name as categoriesname,b.name as companyname from catcomp a ,companies b, categories c 
where a.company_id = b.id and a.category_id = c.id

use this query

There's probably more elegant solutions but anyway...

DROP TABLE IF EXISTS categories;
CREATE TABLE categories 
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,name VARCHAR(12) NOT NULL UNIQUE
);

INSERT INTO categories VALUES
(1 ,'Foo'),(2,'Bar'),(3,'Boo');

DROP TABLE IF EXISTS companies;
CREATE TABLE companies 
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,name VARCHAR(12) NOT NULL UNIQUE
);

INSERT INTO companies VALUES
(1,'Company1'),(2,'Company2'),(3,'Company3');;


DROP TABLE IF EXISTS company_category;
CREATE TABLE company_category
(company_id INT NOT NULL,category_id INT NOT NULL,PRIMARY KEY(company_id,category_id));

INSERT INTO company_category VALUES (1 ,1),(1 ,2),(2 ,2);

SELECT o.id company_id
     , o.name company_name
     , a.id category_id
     , a.name cateory_name
     , CASE WHEN oa.company_id IS NOT NULL THEN ' checked' ELSE '' END checked
  FROM companies o
  JOIN categories a
  LEFT 
  JOIN company_category oa
    ON oa.company_id = o.id
   AND oa.category_id = a.id;

 +------------+--------------+-------------+--------------+----------+
 | company_id | company_name | category_id | cateory_name | checked  |
 +------------+--------------+-------------+--------------+----------+
 |          1 | Company1     |           1 | Foo          |  checked |
 |          2 | Company2     |           1 | Foo          |          |
 |          3 | Company3     |           1 | Foo          |          |
 |          1 | Company1     |           2 | Bar          |  checked |
 |          2 | Company2     |           2 | Bar          |  checked |
 |          3 | Company3     |           2 | Bar          |          |
 |          1 | Company1     |           3 | Boo          |          |
 |          2 | Company2     |           3 | Boo          |          |
 |          3 | Company3     |           3 | Boo          |          |
 +------------+--------------+-------------+--------------+----------+

http://www.sqlfiddle.com/#!2/11e6e/1

Usin example from Strawberry

SELECT categories.name as catename, companies.name as compname from company_category 
INNER JOIN companies on (companies.id=company_category.company_id)
INNER JOIN categories on (categories.id=company_category.category_id) 
where company_category.category_id = categories.id and categories.id=1

I want the boxes to be checked for the current company.

You need in company_category table any status or column with check status, and the query stay:

SELECT categories.name as catename, companies.name as compname from company_category 
INNER JOIN companies on (companies.id=company_category.company_id)
INNER JOIN categories on (categories.id=company_category.category_id) 
where company_category.category_id = categories.id and company_category.status=1

where 'status' is company has been checked or not...

Edit the below query with your table details . I Tried it and its working .

SELECT definition.definition,relation.rtype,word.word FROM definition INNER JOIN relation
ON definition.id = relation.id INNER JOIN word ON definition.id = word.id

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