我知道有几个关于此的帖子,但我无法让这个工作。我没有任何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>
}
这会打印出带有复选框的所有类别。我希望检查当前公司的方框。
有什么想法吗?
答案 0 :(得分: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
使用此查询
答案 1 :(得分: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 | |
+------------+--------------+-------------+--------------+----------+
答案 2 :(得分: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
其中'status'是公司是否已经过检查......
答案 3 :(得分: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