This is my table position:
+----+---------+------+--------+---------+
| id | teacher | cook | doctor | dentist |
+----+---------+------+--------+---------+
| 1 | 3 | 4 | 2 | 1 |
+----+---------+------+--------+---------+
And this is my table people:
+----+-----------+--------+-----+
| id | firstname | name | age |
+----+-----------+--------+-----+
| 1 | Fred | Miller | 42 |
| 2 | Emily | Rose | 32 |
| 3 | Ben | Harper | 38 |
| 4 | Samanta | Jones | 35 |
+----+-----------+--------+-----+
My request from the mySQL database
$pdo = $db->query('
SELECT *, position.id AS id, people.id AS people_id
FROM position
LEFT JOIN people
ON position.teacher=people.id;
ON position.cook=people.id;
ON position.doctor=people.id;
ON position.dentist=people.id;
');
while ($row = $pdo->fetch(PDO::FETCH_ASSOC)) {
echo "The teacher is "$row['firstname']." ".$row['name'];
echo "The cook is "$row['firstname']." ".$row['name'];
echo "The doctor is "$row['firstname']." ".$row['name'];
echo "The dentist is "$row['firstname']." ".$row['name'];
}
My result is:
The teacher is Ben Harper
The cook is Ben Harper
The doctor is Ben Harper
The dentist is Ben Harper
The result I need:
The teacher is Ben Harper
The cook is Samanta Jones
The doctor is Emiliy Rose
The dentist is Fred Miller
It will be better if you restructure your table.
+----+------------+
| id | position |
+----+------------|
| 1 | teacher |
| 2 | cook |
| 3 | doctor |
| 4 | dentist |
+----+------------+
+----+-----------+--------+-----+-------------+
| id | firstname | name | age | position_id |
+----+-----------+--------+-----+-------------+
| 1 | Fred | Miller | 42 | 4 |
| 2 | Emily | Rose | 32 | 3 |
| 3 | Ben | Harper | 38 | 1 |
| 4 | Samanta | Jones | 35 | 2 |
+----+-----------+--------+-----+-------------+
Here you can have a foreign key reference on people.position_id and position.id.
This way you can have many people with same position.
SELECT position.id id
, people.id people_id
, people.firstname
, people.name
FROM position
LEFT
JOIN people
ON people.position_id = position.id;
As noted in comments and answers above, if you can restructure your table, you absolutely should -- as others have mentioned, it's better to restructure it to have that extra column, and then your query will be both simpler and much more efficient. But if you're stuck in a position where you can't (and if you only have a SMALL amount of data as presented, so that the inefficiency of this isn't too much of a concern), then you could use a UNION to glue several simple queries together :
Select position.id, firstname, lastname, 'cook' AS position
FROM position LEFT JOIN people ON position.cook = people.id
UNION
Select position.id, firstname, lastname, 'teacher' AS position
FROM position LEFT JOIN people ON position.teacher = people.id
UNION
...
Again, this is NOT an efficient approach -- you should restructure your data if you can. But if you can't for whatever reason, then this would get you back the data you need in a set of rows that pulls back what you need.
Try this,
$pdo = $db->query('
SELECT *, position.id AS id, people.id AS people_id,
(case when position.teacher <> '' then 'teacher'
when position.cook <> '' then 'cook'
when position.doctor <> '' then 'doctor'
when position.dentist <> '' then 'dentist') position_name
FROM position
LEFT JOIN people
ON position.teacher=people.id;
ON position.cook=people.id;
ON position.doctor=people.id;
ON position.dentist=people.id;
');
while ($row = $pdo->fetch(PDO::FETCH_ASSOC)) {
echo "The ".$row['position_name']." is "$row['firstname']." ".$row['name'];
}
Related
My database looks something like this, in order of user, userteam (connection table) and team:
Usertable UserTeam Teamtable
+--------+------+ +--------+--------+ +--------+-------+
| userid | name | | userid | teamid | | teamid | name |
+--------+------+ +--------+--------+ +--------+-------+
| 1 | eric | | 1 | 1 | | 1 | awsm |
| 2 | john | | 1 | 2 | | 2 | doe |
| 3 | carl | | 2 | 1 | | 3 | empty |
+--------+------+ | 3 | 1 | +--------+-------+
+--------+--------+
How do I select all users that IS NOT in a team, and echo them out only once? I've tried doing it with the usertable, but then it will echo for instance, if I try and select all members not in team 3:
SELECT userid FROM userteam WHERE teamid!=3;
SELECT * FROM user WHERE userid='$previousSql';
1 eric
1 eric
2 john
3 carl
What I'd like instead is:
1 eric
2 john
3 carl
I'm making an add members function, and I'd very much not like everyone to show up more than once.. Any directions, help or guiding would be much appreciated.
add
GROUP BY name
to the query
This should do it:
SELECT u.userid, u.name FROM Usertable u
INNER JOIN UserTeam ut ON ut.userid = u.userid
INNER JOIN Teamtable tt ON tt.teamid = ut.teamid
WHERE tt.teamid != 3
GROUP BY u.userid, u.name;
SELECT userid, name FROM usertable
JOIN userteam ON userteam.userid = usertable.userid
JOIN teamtable on teamtable.teamid = userteam.teamid
WHERE teamtable.teamid = 3;
The SQL above should do the proper joins between all the tables and then will only list the userid/names of people who have a teamid of 3 in the teamtable
EDIT* Removed ! from WHERE clause.
I have two table 'users' and 'friends' I am having difficulty joining them
users table
id | name | usercode
--------------------
1 | david | 2WM
2 | Samme | E5N
3 | Awudu | C0Q
4 | John | VX6
5 | Jerem | FG3
Friends Table
id | actor | target
--------------------
1 | E5N | FG3
2 | 2WM | VX6
3 | FG3 | 2WM
4 | C0Q | VX6
5 | FG3 | VX6
Basically i want to select all users from USERS table who has 'FG3' in either target or actor column in the FRIENDS table.
The result will be
id | name | usercode | actor | target
--------------------------------------
2 | Samme | E5N | E5N | FG3
1 | david | 2WM | FG3 | 2WM
5 | John | VX6 | FG3 | VX6
I have triend everything i know but still i am not getting the correct results
I will be glad if anyone can help me since I need to present this work tomorrow morning. Thank you
Looks like you want to join on usercode equals actor or target, then put the 'FG3' part in a WHERE clause:
SELECT users.id, users.name, users.usercode, friends.actor, friends.target
FROM users
INNER JOIN friends
ON users.usercode = friends.actor OR users.usercode = friends.target
WHERE users.usercode != 'FG3'
AND (friends.actor = 'FG3' OR friends.target = 'FG3');
Using INNER JOIN limits your query to only records that exist in both tables.
I am trying to get some statistics for an online game I maintain. I am searching for an SQL statement to get the result on the bottom.
There are three tables:
A table with teams, each having a unique identifier.
table teams
---------------------
| teamid | teamname |
|--------|----------|
| 1 | team_a |
| 2 | team_x |
---------------------
A table with players, each having a unique identifier and optionally an affiliation to one team by it's unique teamid.
table players
--------------------------------
| playerid | teamid | username |
|----------|--------|----------|
| 1 | 1 | user_a |
| 2 | | user_b |
| 3 | 2 | user_c |
| 4 | 2 | user_d |
| 5 | 1 | user_e |
--------------------------------
Finally a table with events. The event (duration in seconds) is related to one of the players through their playerid.
table events.
-----------------------
| playerid | duration |
|----------|----------|
| 1 | 2 |
| 2 | 5 |
| 3 | 3 |
| 4 | 8 |
| 5 | 12 |
| 3 | 4 |
-----------------------
I am trying to get a result where the durations of all team members is summed up.
result
--------------------------
| teamid | SUM(duration) |
|--------|---------------|
| 1 | 14 | (2+12)
| 2 | 15 | (3+8+4)
--------------------------
I tried several combinations of UNION, WHERE IN, JOIN and GROUP but could not get it right. I am using PostgreSQL and PHP. Can anyone help me?
Just use sum with group by:
select t.teamid, sum(e.duration)
from team t
join players p on t.teamid = p.teamid
join events e on p.playerid = e.playerid
group by t.teamid
If you need all teams to be returned even if they don't have events, then use an outer join instead.
Try this
SELECT teamid, Sum(duration),
AS LineItemAmount, AccountDescription
FROM teams
JOIN teams ON teams.teamid = players.teamid
JOIN events ON players.playersid = events.playersid
JOIN GLAccounts ON InvoiceLineItems.AccountNo = GLAccounts.AccountNo
GROUP BY teamid
http://www.w3computing.com/sqlserver/inner-joins-join-two-tables/
select * from Teams;
| team_name | team_id |
| India | 1 |
| England | 2 |
| Germany | 3 |
| Japan | 4 |
select * from Matches;
| match_id | match_date | hometeam | awayteam | homescore | awayscore |
| 1 | 2014-06-24 | 1 | 2 | 3 | 0 |
| 2 | 2014-06-25 | 3 | 4 | 0 | 2 |
Matches-table hometeam and awayteam are foreign keys for Teams-table team_id
I want a select that displays the homescore and awayscore along with team_names
You have to join matches table with teams table like this:
SELECT th.team_name as team_home, m.homescore, ta.team_name as team_away, m.awayscore
FROM matches m
INNER JOIN teams th ON m.hometeam = th.team_id
INNER JOIN teams ta ON m.awayteam = ta.team_id
Here is SqlFiddle
Another one, with entire match data in one select...
select
m.*,
home.team_name as hometeam_name,
away.team_name as awayteam_name
from
matches m,
teams home,
teams away
where
home.team_id = m.hometeam
and
away.team_id = m.awayteam
;
see SQLFIDDLE
I have two tables that I would like to join into one, but not all the fields. I've used INNER JOIN with some success, but can't get the exact results I need. Essentially, when using PHP to return results, I would like the key to be the 'meta_key' value.
Below are the two tables I want to combine:
USERS
+--------+-----------------+------------------+----------------+
| ID | username | first_name | last_name |
+--------+-----------------+------------------+----------------+
| 2 | hthompson | Hunter | Thompson |
| 7 | coak | Carol | Oak |
| 8 | delk | Dannie | Elk |
| 9 | mride | Mark | Ride |
| 10 | kken | Kyle | Ken |
| 11 | glee | Ginny | Lee |
| 12 | nwatts | Naomi | Watts |
| 13 | jwong | Jin | Wong |
| 14 | syin | Shen | Yin |
+--------+-----------------+------------------+----------------+
USERS_META
+--------+--------+-----------------+------------------+
| ID | UID | meta_key | meta_value |
+--------+--------+-----------------+------------------+
| 1 | 2 | business_name | Company Inc. |
| 2 | 2 | city | New York |
| 3 | 2 | state | NY |
| 5 | 9 | city | Boston |
| 6 | 9 | state | MA |
| 7 | 11 | business_type | Printer |
| 8 | 8 | chamber_member | true |
| 9 | 2 | business_type | Design |
+--------+--------+-----------------+------------------+
Below is an example of what I'd like to return:
USERS
+--------+-----------------+------------+------------+------------------+
| ID | username | city | state | business_name |
+--------+-----------------+------------+------------+------------------+
| 2 | hthompson | New York | NY | Company Inc. |
+--------+-----------------+------------+------------+------------------+
OR
$user['ID'] = 2
$user['username'] = hthompson
$user['city'] = New York
$user['state'] = NY
$user['business_name'] = Company Inc.
The closest I've come is this:
$query = ("SELECT *
FROM users
INNER JOIN users_meta ON users.ID = users_meta.UID
WHERE
users_meta.meta_key = 'city' OR
users_meta.meta_key = 'state' OR
users_meta.meta_key = 'business_name'
");
However, doing such returns three results for each unique user ID, and I'm aiming to returning one with all the meta info specified. The primary purpose of this is so that I will be able to search using a keyword, which would apply to the USERS.first_name, USERS.first_name and USERS_META.business_name columns and then obviously return results in a table showing ID, Business Name, City, State, First & Last Name.
Thanks in advance!
You can try this:
SELECT
u.ID,
u.username,
m0.meta_value as city,
m1.meta_value as state,
m2.meta_value as business_name
FROM
users u,
users_meta m0,
users_meta m1,
users_meta m2
WHERE
u.ID = m0.UID
AND m0.meta_key = 'city'
AND u.ID = m1.UID
AND m1.meta_key = 'state'
AND u.ID = m2.UID
AND m2.meta_key = 'business'
As far as getting the information out I would just suggest joining the table together and using php to make the array. Something similar to this:
<?php
//if search is blank, returns all rows
$search = isset($_POST['search'])?mysql_real_escape_string($_POST['search']):'';
//query for all rows with meta key/values including extra
//rows from the join. extra rows will be taken out later.
$result = mysql_query(
"SELECT
U.ID,
U.USERNAME,
U.FIRST_NAME,
U.LAST_NAME,
UM.META_KEY,
UM.META_VALUE
FROM USERS U
JOIN USERS_META UM ON U.ID=UM.UID
WHERE
UM.META_KEY IN ('city', 'state', 'business_name')
AND U.FIRST_NAME LIKE '%{$search}%'");
//an empty array to put our results into
$out = array();
//loop through the rows
while($row=mysql_fetch_assoc($out)){
//check if the user id has been added already
if(!isset($out[$row['ID']])){
//if not, add it with generic information
$out[$row['ID']] = array(
'ID'=>$row['ID'],
'USERNAME'=>$row['USERNAME'],
'FIRST_NAME'=>$row['FIRST_NAME'],
'LAST_NAME'=>$row['LAST_NAME']);
}
//add the meta key and value
$out[$row['ID']][$row['META_KEY']] = $row['META_VALUE'];
}
//display
echo '<pre>'.print_r($out,1).'</pre>';
?>