我正在使用Joomla 2.5。我正在使用MYSQL数据库。
我有一个表job_field
,其中包含以下列:
cat_id | location_id
-----------------------
1,4 | 66,70
我需要将它与另一个表job_value
cat_id | location_id | name
--------------------------------
1 | 70 | Atul
4 | 70,80 | Amit
4 | 80,66 | Amol
1 | 66 | Pritam
3 | 70 | Rahul
2 | 66,90 | Ajit
1 | 74 | Raju
4 | 65,22 | Manoj
我希望输出将第一个表cat_id
中的location_id
和job_details
列与第二个表job_value
列cat_id
和location_id
进行比较。< / p>
它将与第2个表(job_details
)location_id
列分别检查第1个表(job_value
)中location_id
列值(66,70)的每个值。我将输出数组作为
Array (
1 70 Atul
4 70,80 Amit
4 80,66 Amol
1 66 Pritam
)
答案 0 :(得分:3)
这是一个 坏坏坏 结构。即使问题可以解决,也不应该解决。它会很慢,而且不可维护。
应该为DB的这一部分创建沿着这些线的东西,而不是糟糕的结构:
CREATE TABLE PERSON (
person_id BIGINT,
name VARCHAR(64),
PRIMARY KEY (person_id)
);
CREATE TABLE LOCATION (
location_id BIGINT,
name VARCHAR(64),
PRIMARY KEY (location_id)
);
CREATE TABLE CAT (
cat_id BIGINT,
name VARCHAR(64),
PRIMARY KEY (cat_id)
);
CREATE TABLE CAT_LOCATION (
cat_id BIGINT,
location_id BIGINT,
PRIMARY KEY (cat_id,location_id),
FOREIGN KEY (cat_id) REFERENCES cat(cat_id),
FOREIGN KEY (location_id) REFERENCES location(location_id)
);
CREATE TABLE CAT_LOCATION_PERSON (
cat_id BIGINT,
location_id BIGINT,
person_id BIGINT,
PRIMARY KEY (cat_id,location_id,person_id),
FOREIGN KEY (cat_id) REFERENCES cat(cat_id),
FOREIGN KEY (location_id) REFERENCES location(location_id),
FOREIGN KEY (person_id) REFERENCES person(person_id)
);
然后通过简单的连接获得你想要的东西比简单容易:
SELECT cl.cat_id, cl.location_id, p.name
FROM CAT_LOCATION cl
JOIN CAT_LOCATION_PERSON clp on cl.cat_id = clp.cat_id and cl.location_id=clp.location_id
JOIN PERSON p on clp.person_id = p.person_id
(我拒绝编写一个查询,该查询将提供指定格式的输出,数字值以逗号分隔...(尽管可以通过MySQL的GROUP_CONCAT功能轻松实现))
答案 1 :(得分:0)
试试这个::
SELECT
*
FROM table1
JOIN table2 ON FIND_IN_SET(table1.location_id, table2.location_id) > 0