
July 16th, 2012, 01:24 AM
|
|
Contributing User
|
|
Join Date: Apr 2012
Posts: 48
Time spent in forums: 10 h 2 m 36 sec
Reputation Power: 2
|
|
|
Help with a SQL multi column query
hello everybody
i am still a bit confused by SQL queries and very desperate to learn. so i really appriciate your advice on where i have gone wrong with this query.
i am using a MySQL database.
i have an employment database table. - table one is the - user Id table - this is the table containing the Id of members.
- table two is the list of skills sets.
i.e
first degree skills_id 1
masters degree skills_id 2
profssional qualifications. skills_id 3
- table three is the link between table one and two.
ie.
USE workstable ;
CREATE TABLE skills_list(
skills_id MEDIUMINT UNSIGNED NOT NULL,
user_id MEDIUMINT UNSIGNED NOT NULL,
INDEX skills_id( skills_id, user_id ),
INDEX user_id ( user_id, skills_id)
);
A member might have several entries for the skills list table.
The problem however is that when i use an SQL query to query the 'skills_list' table i am only getting one result per member.
it might help at this point if i show you my query;
PHP Code:
function worker_list ( $user_id )
{
global $dbc;
$select = " SELECT
u.name,
u. date_joined,
sk.skills_id,
la.languages_id
";
$from = " FROM
users u LEFT OUTER JOIN skills_list sk
ON u.user_id = sk.user_id
users u LEFT OUTER JOIN languages_list la
ON u.user_id = la.user_id
";
$where = sprintf(" WHERE
u.user_id = '%s' ",
mysqli_real_escape_string($dbc,$user_id)
);
$query = $select.$from.$where ;
$result = mysqli_query ($dbc, $query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysqli_error($dbc));
return $result ;
}
//please note that the languages list is similar to the skills_list column. i.e a member can have several entries for all the languages that he speaks
the above query should have given me several entries for the users langauge and skills entries; instead it returns only the first entry s for each of the column.
if however i only query for one item in my query, it produces the correct result; ie.
PHP Code:
function worker_list ( $user_id )
{
global $dbc;
$select = " SELECT
skills_id
";
$from = " FROM
skills_list
";
$where = sprintf(" WHERE
u.user_id = '%s' ",
mysqli_real_escape_string($dbc,$user_id)
);
$query = $select.$from.$where ;
$result = mysqli_query ($dbc, $query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysqli_error($dbc));
return $result ;
}
The above query will return all the entries for a user within the skill set table.
So i am unclear where i went wrong when i tried to query several tables at the same time; should i have used a 'Group by Clause'. if so, how do i draft it.
thank you for your kind attention.
warm regards
Andreea
|