Hi Everyone,
This question follows on from the resolution of an early question in:
http://www.dbforums.com/mysql/168804...ml#post6576815
Speical thanks to R937 and healdem
My question now relates to how to correctly draft a query from a table that has two types of relationships: 1-1 relationship , 1 to many.
It will be easier if i explain the table design.
It's a members database containing over 60 individual tables relating to it's members profiles; i.e languagage skills, education etc;
so a member might have numrous language skills (1 to many), visited numrous countries (1 to many) but now lives in 1 country ( 1-1).
There are 3 types of tables ;
I now want to build an SQL query to search the database. Below is a part sample of my query:
the problem with the way i have structured the query is that it is leading to duplicate results.
for example , if user_id 1 speak 6 languauges, has 9 diffrent skills sets, and have visited 20 countries then the result will produce
20 x 6 instances of the languauge Id plus
9 x 6 instnace of the langauge id etc
this leads to thousand of results which slows my query down to a snails pace.
i am unclear where i have gone wrong.
i tried using GROUP_CONCAT along with GROUP BY but this also produced false results.
Below is a sample of the database tables. they are all pretty much like this. i.e a table listing the qualifications-along with its Id and then a link table linking the user to the list.
i would really appriciate any advice assitance on how to proceed
This question follows on from the resolution of an early question in:
http://www.dbforums.com/mysql/168804...ml#post6576815
Speical thanks to R937 and healdem
My question now relates to how to correctly draft a query from a table that has two types of relationships: 1-1 relationship , 1 to many.
It will be easier if i explain the table design.
It's a members database containing over 60 individual tables relating to it's members profiles; i.e languagage skills, education etc;
so a member might have numrous language skills (1 to many), visited numrous countries (1 to many) but now lives in 1 country ( 1-1).
There are 3 types of tables ;
- User- the member's table i.e containing the user_id of members.
- Data Tables- table containing data lists i.e list of countries, list of qualifications etc
- Link Table - this is the link between the users and data tables. i.e the id of the user linked to the Id of the datatable entry.
I now want to build an SQL query to search the database. Below is a part sample of my query:
PHP Code:
SELECT
u.user_id,
u.first_name,
(wl.language_id)fleuntlang,
(wml.language_id)mediumlang,
(wag.age_id)as ageid,
( wc.job_id)as jobid
FROM
users u
LEFT OUTER JOIN members_timetable mt
ON u.user_id = mt.user_id
LEFT OUTER JOIN workers_languagelist wl
ON u.user_id = wl.user_id
LEFT OUTER JOIN workers_medium_languagelist wml
ON u.user_id = wml.user_id
LEFT OUTER JOIN workers_agegroup wag
ON u.user_id = wag.user_id
LEFT OUTER JOIN worker_category wc
ON u.user_id = wc.user_id
for example , if user_id 1 speak 6 languauges, has 9 diffrent skills sets, and have visited 20 countries then the result will produce
20 x 6 instances of the languauge Id plus
9 x 6 instnace of the langauge id etc
this leads to thousand of results which slows my query down to a snails pace.
i am unclear where i have gone wrong.
i tried using GROUP_CONCAT along with GROUP BY but this also produced false results.
PHP Code:
SELECT
u.user_id,
u.first_name,
GROUP_CONCAT( wl.language_id)fleuntlang,
GROUP_CONCAT(wml.language_id)mediumlang,
GROUP_CONCAT( wag.age_id)as ageid,
GROUP_CONCAT( wc.job_id)as jobid
FROM
users u
LEFT OUTER JOIN members_timetable mt
ON u.user_id = mt.user_id ......
.................
GROUP
BY u.user_id, wl.language_id, wml.language_id, wc.job_id
i would really appriciate any advice assitance on how to proceed
PHP Code:
CREATE TABLE users(
user_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
membership_type CHAR(3) NOT NULL,
PRIMARY KEY (user_id)
}
CREATE TABLE countrylist
country_id MEDIUMINT UNSIGNED NOT NULL,
country VARCHAR(60) NOT NULL,
INDEX country_id ( country_id, country ),
INDEX countrylist (country, country_id ),
UNIQUE KEY (country)
CREATE TABLE countrylocation(
country_id VARCHAR(60) NOT NULL,
user_id MEDIUMINT UNSIGNED NOT NULL,
INDEX country_id (country_id, user_id ),
INDEX user_id (user_id, country_id )
);
The values in teh countrylist
(1 , 'African' ),
(2 , ' Albanian' ),
(3 , 'Saudia Arabia ' ),
(4 , ' Armenia' ),
(5 , ' Australia' ),
(6 , ' Austria' ),