I have two tables, namely, manager and employee. I have managed to successfully implement a foreign key constraint on employee table and and everything seems to be working fine. However, whenever I query the database to find out which manager is allocated to which employee using INNER JOIN, the result it gives me is limited to the number of manager ids I have. For example if 2 particular employees are allocated to the same manager, it only gives me one result in terms of the list of employee's who is allocated to the same manager.
I tried using LEFT JOIN to see whether the foreign I want to impelement are actually inserted. To my surprise it gives me the result of NULL for columns which explicitly have the same foreign key as those ones that is actually working. Sorry in advance if you find my description unclear as English is not my first language. However, to give you a better idea of what im trying to explain my tables are as follows
employee Manager
emploee ID | firstname | lastname | managerid managerid |fname | lastname
1 john doe 1 1 gordon soo
2 ian lee 1 2 justin freeman
3 faye eva 2 3 sai chow
What happens when I try LEFT JOIN:
Employee name |lasttname | ManagerNAme
john doe gordon
ian lee NUll
I have tried WHERE clause as well a checked if both tables are on INODB and have the same collation but I just cant seem to fix it. Hope you can help.
As far as I understand the question, you need a simple JOIN on the tables. As you don't show your queries, it is hard to say what's wrong. This is probably what you want:
SELECT
e.firstname, e.lastname, m.fname
FROM
Manager m
JOIN employee e ON e.managerid = m.managerid
Related
At first I have to say that I am an absolute SQL newbie.
I have two SQL tables. The second one is an intermediary m:n table.
Table: people
id
name
1
Yoda
2
Anakin
3
Luke
4
Obi-Wan
Table: master-padawan_relation
master_id
padawan_id
1
3
4
2
master-padawan_relation.master_id and master-padawan_relation.padawan_id both refer to people.id.
Now I want a query that shows the following data:
Master
Padawan
Yoda
Luke
My query looks currently like this but it lacks the integration of the Masters name:
SELECT
people.name AS Padawan
FROM
people
LEFT JOIN
master-padawan_relation ON master-padawan_relation.padawan_id = people.id
WHERE
people.id = 3
I don't know how to get that Masters name in there, because both names are in the same column (people.name).
I also managed to get the masters id, but I really need that name.
Appreciate every help! Thank you :)
I have persons (table person) who have 0 or N roles (tables role and personne_role).
I want to select all the persons , with the roles they have, to have this kind of result :
PHIL COLLINS | Drummer | Singer
MIKE RUTHERFORD | Singer
ION ANDERSON | Singer
MIKE JAGGER |
CARLOS SANTANA | Guitarist
......
Each line can have 0 or N roles.
To do that, I make 2 requests
the first one to get the employees (table person)
the second one to loop all the retrieved employees and retrieve each role of them (tables role and person_role)
It works BUT in the case of there are a lot of lines, it is not very efficient.
I would like the same result in 1 request.
Is it possible ?
What are the mysql keywords I must use to do that ?
Thanks for your feedback.
dominique
You could use a JOIN with a GROUP_CONCAT, something like:
SELECT person.name, role.roles
FROM person
LEFT JOIN (
SELECT person_id, GROUP_CONCAT(DISTINCT role SEPARATOR ' | ') roles
FROM person_role
GROUP BY person_id
) role ON (person.id = role.person_id)
EDIT: the fields name are just a guess, since you didn't show us the full table schema; also, if the roles are actually in a separate tale, say joined by a role_id, you'd need to add it to the subquery.
I am very new to SQL. I have done some basic "select from where ..." queries but I struggle with my current project.
Lets say this is my source table:
Project Involved
1 Harald
1 Kerstin
1 Peter
1 Christian
1 Lisa
1 Linda
2 Sören
2 Schmidt
2 Jörg
2 Robert
2 Harald
2 Lisa
My question should be fairly simple. The input is the name "Lisa" and "Harald". I want to know "Which projects are Lisa and Harald involved in"
If this is super easy and cannot understand why I ask such easy thing: provide me with a link where this is explained and ill read trough it myself, just not so sure what exactly to look for so I thought this was a faster way to get started :)
You can solve this in many ways, but here is the primary way I would solve it... but start simply for what projects LISA is associated with... This prevents looking at what could be 1000s of projects / people but if Lisa is only associated with 5 (or 2 in this case), why query against all of them..
Select
p1.project
from
project p1
where
p1.involved = 'Lisa'
So this lists projects 1 & 2 (obviously your short sample of data. Now, that we know this works, I would just JOIN again for Harold and the same project as this.
Select
p1.project
from
project p1
join project p2
on p1.project = p2.project
AND p2.involved = 'Harold'
where
p1.involved = 'Lisa'
Ensure you have an index on (involved, project) to help optimize the query
Additionally, others may propose to do a group by and having clause based on both parties you are interested in.... something like
select
p1.project
from
project p1
where
p1.involved in ( 'Lisa', 'Harold')
group by
p1.project
having
count(*) = 2
This basically says to the engine. Give me each project where either Lisa or Harold exist. But, by applying a group by I only want the project to show once so I don't see duplicates. The HAVING clause tells how many you EXPECT to have per project, and since you are asking for 2 possible names, and want both of them, the HAVING COUNT(*) is 2 so you know BOTH are included.
I've named table as person, so this is the example
SELECT
p1.project
FROM
person p1,
person p2
WHERE
p1.name = 'Harald'
AND p2.name = 'Lisa'
AND p1.project = p2.project
To see the projects which both Lisa and Harald are involed in:
SELECT P1.Project FROM
(SELECT Project FROM MyTable WHERE Involved = 'Lisa') P1
INNER JOIN
(SELECT Project FROM MyTable WHERE Involved = 'Harald') P2
WHERE P1.Project = P2.Project
I am VERY new to SQL and do not know that much about it, but I am a quick learner. I have a database with Item IDs and Quantities on Hand (qoh) along with some other columns that I am not having issues with. The problem is that when I tell it to give me the item id column with the qoh column, it gives me almost 500 rows for each individual item because the qoh is different so I literally have about 12 million rows. Now, what I am looking for is the most recent quantity for each item. I am assuming that this is a one-to-many relationship and since I am below a noobie to SQL, I don't even really know where to begin. This is what I have so far:
SELECT DISTINCT item_id, qty_on_hand
FROM database.inv_mast, database.inv_loc
ORDER BY item_id ASC
Oh and I'm using Microsoft SQL Server Management Studio
Here is what I want:
| item_id |qty_on_hand|
|123456789| 93 |
|456789123| 87 |
|789456123| 74 |
etc.
But what I'm getting is:
| item_id |qty_on_hand|
|123456789| 85 |
|123456789| 82 |
|123456789| 92 |
etc.
I am getting the same item_id at least 4000 times because the database is telling me what the qoh was for literally every second of time since we've had the item. I only want what is in the warehouse at the time of me running the query. I apologize for all the noobness, but I literally don't know SQL.
You are doing a cross join by just using ",". What you need to do is a inner join I think.
SELECT DISTINCT im.item_id, qty_on_hand
FROM database.inv_mast im
INNER JOIN database.inv_loc in on im.item_id = in.item_id
ORDER BY item_id ASC
You also should look at left and right joins.
So in summary using "," is like using a cross join
You really need to learn joins to understand these queries, check out the site below.
http://www.w3schools.com/sql/sql_join.asp
The from clause FROM database.inv_mast, database.inv_loc creates the cartesian product. Narrow that down using a WHERE condition.
I have two tables.
Table Emp
id name
1 Ajay
2 Amol
3 Sanjay
4 Vijay
Table Sports
Sport_name Played by
Cricket ^2^,^3^,^4^
Football ^1^,^3^
Vollyball ^4^,^1^
Now I want to write a query which will give me output like
name No_of_sports_played
Ajay 2
Amol 1
Sanjay 2
Vijay 2
So what will be Mysql query for this?
I agree with the above answers/comments that you are not using a database for what a database is for, but here is how you could calculate your table from your current structure in case you have no control over that:
SELECT Emp.name, IF(Played_by IS NULL,0,COUNT(*)) as Num_Sports
FROM Emp
LEFT JOIN Sports
ON Sports.Played_by RLIKE CONCAT('[[:<:]]',Emp.id,'[[:>:]]')
GROUP BY Emp.name;
See it in action here.
UPDATE: added the IF(Played_by IS NULL,0,COUNT(*)) instead of COUNT(*). This means that if an employee doesn't play anything they'll have a 0 as their Num_Sports. See it here (I also added in those ^ characters and it still works.
What it does is joins the Emp table to the Sports table if it can find the Emp.id in the corresponding Played_by column.
For example, if we wanted to see what sports Ajay played (id=1), we could do:
SELECT *
FROM Emp, Sports
WHERE Sports.Played_by LIKE '%1%'
AND Emp.id=1;
The query I gave as my solution is basically the query above, with a GROUP BY Emp.name to perform it for each employee.
The one modification is the use of RLIKE instead of LIKE.
I use RLIKE '[[:<:]]employeeid[[:>:]]' instead of LIKE '%employeeid%. The [[:<:]] symbols just mean "make sure the employeeid you match is a whole word".
This prevents (e.g.) Emp.id 1 matching the 1 in the Played_by of 3,4,11,2.
You do not want to store your relationships in a column like that. Create this table:
CREATE TABLE player_sports (player_id INTEGER NOT NULL, sport_id INTEGER NOT NULL, PRIMARY KEY(player_id, sport_id));
This assumes you have an id column in your sports table. So now a player will have one record in player_sports for each sport they play.
Your final query will be:
SELECT p.name, COUNT(ps.player_id)
FROM players p, player_sports ps
WHERE ps.player_id = p.id
GROUP BY p.name;