0

我知道有几个关于这个的帖子,但我无法让它工作。我没有任何MySQL“加入”或“离开”的经验,只有简单的查询。

我有 3 个表:类别、公司和 catcomp

类别

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

1 | Foo

2 | Bar

公司

id | name | ... | ...

1 | Company1

2 | Company2

Catcomp(为一家公司存储多个类别)

company_id | category_id

1 | 1

1 | 2

2 | 2

我只有这个:

$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>
}

这会打印出所有带有复选框的类别。我希望为当前公司选中这些框。

有任何想法吗?

4

4 回答 4

1
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

使用这个查询

于 2013-08-09T13:13:08.440 回答
1

可能有更优雅的解决方案,但无论如何......

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

于 2013-08-09T13:28:34.853 回答
0

使用您的表详细信息编辑以下查询。我试过了,它的工作原理。

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
于 2013-08-12T14:02:46.457 回答
0

使用草莓的例子

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

我希望为当前公司选中这些框。

您需要 company_category 表中的任何状态或具有检查状态的列,并且查询保持:

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

其中“状态”是公司是否已检查...

于 2013-08-12T13:40:40.843 回答