I have two tables in my database.
Men table:
+----------------------------------------
| ID | menname | partner |
+----------------------------------------
| 1 | Mark | 1 |
| 2 | Adam | 2 |
----------------------------------------+
Women table:
+---------------------------------------+
| ID | womenname |
+---------------------------------------+
| 1 | Lisa |
| 2 | Emma |
+---------------------------------------+
When I do a inner join, I get:
+----------------------------------------
| ID | menname | partner |
+----------------------------------------
| 1 | Mark | 1 |
| 2 | Adam | 2 |
----------------------------------------+
instead of:
+----------------------------------------
| ID | menname | partner |
+----------------------------------------
| 1 | Mark | Lisa |
| 2 | Adam | Emma |
----------------------------------------+
It gets the id, instead of the partner name.
Anyone know what is wrong with my query?
SELECT m.ID
,m.menname
,w.womenname AS partner
FROM Men AS m
INNER JOIN Women AS w ON m.partner = w.ID;
Related
How to join these to in one sql statement?
I have these 2 simple tables.
3_Referee_Matches
+------------------------------------+
| ID | CountryCode | RefereeUrlCode |
| 1 | eng | mike-jean |
| 2 | eng | mike-jean |
| 3 | eng | mike-jean |
| 4 | eng | mike-jean |
| 5 | spa | hulo-pape |
| 6 | ita | enri-tolsi |
| 7 | ita | enra-ean |
| 8 | ita | enra-ean |
+------------------------------------+
3_Players
+----------------------------------------------------+
| ID | MatchID | Name | PlayerUrlCode | Yellow |
| 1 | 1 | Mike Bell | mike-bell | 1 |
| 2 | 2 | Mike Bell | mike-bell | 1 |
| 3 | 3 | Thoms Tim | thoms-tim | 1 |
| 4 | 4 | Jean Claod | jean-claod | 0 |
| 5 | 33 | Thoms Tim | thoms-tim | 1 |
| 6 | 44 | Fis Most | fis-most | 0 |
| 7 | 54 | Geni Toens | geni-toens | 1 |
| 8 | 67 | Geni Toens | geni-toens | 1 |
+----------------------------------------------------+
Today i use these 2 select. But need help to combine them into one.
select 1:
SELECT rm.*, p.PlayerUrlCode AS VALUEtoBEusedAGAIN, COUNT(p.ID) AS YellowCounter
FROM 3_Referee_Matches rm
JOIN 3_Players p ON rm.ID = p.MatchID
WHERE rm.CountryCode = 'eng' AND rm.RefereeUrlCode = 'mike-jean'
AND p.Yellow>0
GROUP BY p.Name
select 2:
SELECT COUNT(rm.ID) AS Counter
FROM 3_Referee_Matches rm
JOIN 3_Players p ON rm.ID = p.MatchID
WHERE rm.RefereeUrlCode='mike-jean'
AND p.PlayerUrlCode='VALUEtoBEusedAGAIN'
Result should be like this:
+--------------------------------------+
| Name | YellowCounter | Counter |
| Mike Bell | 2 | 2 |
| Jean Claod | 1 | 1 |
+--------------------------------------+
Showing that Mike Bell Got 2 yellow cards in 2 matches.
Your first query is nearly there. First, remove the extraneous columns from the select clause. count the number of "players" (really player information for each match) and sum their yellow cards.
select
p.name,
sum(yellow) as YellowCounter,
count(p.id) as Counter
from 3_Referee_Matches rm
join 3_Players p on rm.ID = p.MatchID
where rm.CountryCode = 'eng'
and rm.RefereeUrlCode = 'mike-jean'
group by p.name;
+------------+---------------+---------+
| name | YellowCounter | Counter |
+------------+---------------+---------+
| Mike Bell | 2 | 2 |
| Thoms Tim | 1 | 1 |
| Jean Claod | 0 | 1 |
+------------+---------------+---------+
I assume the example has Thoms and Jean reversed.
student table
|----------------------|
| student_id | name |
|------------|---------|
| 1 | Richard |
| 2 | Emily |
| 3 | Hans |
|------------|---------|
lecturer table
|--------------------|
| lecturer_id | name |
|-------------|------|
| 1 | John |
| 2 | Mike |
|-------------|------|
classes table
|-----------------------------------------------|
| class_id | lecturer_id | material |
|----------|-------------|----------------------|
| 1 | 1 | Basic of algorithm |
| 2 | 1 | Basic of programming |
| 3 | 2 | Database Essentials |
| 4 | 2 | Basic of SQL |
|----------|-------------|----------------------|
attendance table
|-----------------------|
| class_id | student_id |
|----------|------------|
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 2 | 2 |
| 3 | 1 |
| 3 | 2 |
| 3 | 3 |
| 4 | 1 |
| 4 | 2 |
|----------|------------|
how to show classes records (from classes table) that not attended by Hans (student) in MySQL?
desired result :
|-----------------------------------------------|
| class_id | lecturer_id | material |
|----------|-------------|----------------------|
| 2 | 1 | Basic of programming |
| 4 | 2 | Basic of SQL |
|----------|-------------|----------------------|
One approach uses EXISTS:
SELECT c.class_id, c.lecturer_id, c.material
FROM classes c
WHERE NOT EXISTS (SELECT 1 FROM attendance a
INNER JOIN student s
ON a.student_id = s.student_id
WHERE a.class_id = c.class_id AND
s.name = 'Hans');
Using joins -
select c.class_id
from attendance a inner join student s on (a.student_id=s.student_id and s.student_id='Hans')
right outer join classes c on (a.class_id=c.class_id)
where a.class_id is null
I have three tables college_details, college_courses, course of which college_courses is an intermediate table for many to many relation ships between college_details and course.
college_details
+------------+--------------+
| college_id | college_name |
+------------+--------------+
| 1 | abc, Nagpur |
| 2 | xyz, Nagpur |
| 3 | mnop, Nagpur |
+------------+--------------+
college_courses
+------------+--------------+
| college_id | course_id |
+------------+--------------+
| 1 | 1 |
| 1 | 5 |
| 2 | 3 |
| 3 | 5 |
+---------------------------+
course
+----------+-------------+
|course_id | course_name |
+----------+-------------+
| 1 | B.E |
| 2 | M.E |
| 3 | M.Tech |
| 4 | B.Tech |
| 5 | M.B.A |
+----------+-------------+
SELECT cd.college_id
, cd.college_name
, c.course_name
FROM college_details AS cd
LEFT
JOIN college_courses AS cc
ON cc.college_id = cd.college_id
LEFT
JOIN course AS c
ON cc.course_id = c.course_id
From the above query I get the following table.
+------------+---------------+-------------+
| college_id | college_name | course_name |
+------------+---------------+-------------+
| 1 | abc, college | be |
| 1 | abc, college | mba |
| 2 | xyz, college | me |
| 3 | mnop, college | btech |
+------------+---------------+-------------+
query -> "SELECT cd.college_id,cd.college_name,GROUP_CONCAT(c.course_name) FROM college_details AS cd LEFT JOIN college_courses AS cc ON cc.college_id = cd.college_id LEFT JOIN course AS c ON cc.college_id = c.course_id ";
And from above query I get the following table.
+------------+--------------+-----------------------------+
| college_id | college_name | GROUP_CONCAT(c.course_name) |
+------------+--------------+-----------------------------+
| 1 | abc, college | be,be,me,btech |
+------------+--------------+-----------------------------+
But what I want is the following table.
+------------+---------------+-------------+
| college_id | college_name | course_name |
+------------+---------------+-------------+
| 1 | abc, college | be,mba |
| 2 | xyz, college | me |
| 3 | mnop, college | btech |
+------------+---------------+-------------+
I have a table "user" like this :
| uid | username |
| 1 | Jack |
| 2 | John |
| 3 | Robert |
Using my request :
SELECT u1.username, u2.username FROM user u1 JOIN user u2
Using this request, I get a table like this :
| uid | username | uid | username |
| 1 | Jack | 1 | Jack |
| 1 | Jack | 2 | John |
| 1 | Jack | 3 | Robert |
| 2 | John | 1 | Jack |
| 2 | John | 2 | John |
| 2 | John | 3 | Robert |
| 3 | Robert | 1 | Jack |
| 3 | Robert | 2 | John |
| 3 | Robert | 3 | Robert |
How can I remove one of these duets?
| 1 | Jack | 2 | John |
| 2 | John | 1 | Jack |
You want all combinations of User table 1 with User table 2. I believe this would accomplish this:
SELECT u1.id, u1.name, u2.id u2.name
FROM User u1
INNER JOIN User u2
WHERE u1.id >= u2.id
This would give the following result:
| uid | username | uid | username |
| 1 | Jack | 1 | Jack |
| 2 | John | 1 | Jack |
| 2 | John | 2 | John |
| 3 | Robert | 1 | Jack |
| 3 | Robert | 2 | John |
| 3 | Robert | 3 | Robert |
I have three tables:
Person
+--------+-----------+
| fName | lName |
+--------+-----------+
| Paul | McCartney |
| John | Lennon |
| Jon | Stewart |
| Daniel | Tosh |
| Steven | Colbert |
| Pink | Floyd |
| The | Beatles |
| Arcade | Fire |
| First | Last |
| Andrew | Bird |
+--------+-----------+
Publication
+----+---------------------------------------+------+-----------+---------+
| id | title | year | pageStart | pageEnd |
+----+---------------------------------------+------+-----------+---------+
| 9 | The Dark Side of the Moon | 1973 | 0 | 0 |
| 10 | Piper At The Gates of Dawn | 1967 | 0 | 0 |
| 11 | Sgt. Pepper's Lonely Hearts Band Club | 1967 | 0 | 0 |
| 12 | Happy Thoughts | 2007 | 0 | 60 |
| 13 | Wish You Were Here | 1975 | 0 | 0 |
| 14 | Funeral | 2004 | 0 | 0 |
+----+---------------------------------------+------+-----------+---------+
Person_Publication
+-----------+----------------+--------+---------------+
| person_id | publication_id | editor | author_number |
+-----------+----------------+--------+---------------+
| 11 | 11 | 0 | 1 |
| 12 | 11 | 0 | 1 |
| 16 | 9 | 0 | 1 |
| 17 | 11 | 0 | 1 |
+-----------+----------------+--------+---------------+
I'm trying to select all authors of a certain publication using the following query:
SELECT fName , lName
FROM Publication , Person, Person_Publication
WHERE Person.id = Person_Publication.person_id
AND Person_Publication.publication_id = 11;
But the results I get are always duplicates (always 6x for some reason). The results:
+-------+-----------+
| fName | lName |
+-------+-----------+
| Paul | McCartney |
| John | Lennon |
| The | Beatles |
| Paul | McCartney |
| John | Lennon |
| The | Beatles |
| Paul | McCartney |
| John | Lennon |
| The | Beatles |
| Paul | McCartney |
| John | Lennon |
| The | Beatles |
| Paul | McCartney |
| John | Lennon |
| The | Beatles |
| Paul | McCartney |
| John | Lennon |
| The | Beatles |
+-------+-----------+
18 rows in set (0.03 sec)
Can somebody please tell me why this is happening and how to fix this?
You are getting 6x your results, exactly one for each Publication row.
Remove your Publication from your FROM clause:
SELECT fName , lName
FROM Person, Person_Publication
WHERE Person.id = Person_Publication.person_id
AND Person_Publication.publication_id = 11;
You are including three tables in your query:
FROM Publication, Person, Person_Publication
but you have only one join condition:
WHERE Person.id = Person_Publication.person_id
You end up with a cartesian product between Publication and Person JOIN Person_Publication
Add the following condition to your WHERE block:
AND Publication.id = Person_Publication.publication.id
A perfect example of why the explicit JOIN syntax is prefered. With the following syntax:
SELECT fName, lName
FROM Publication
JOIN Person_Publication ON Person_Publication.publication.id = Publication.id
JOIN Person ON Person.id = Person_Publication.person_id
WHERE Person_Publication.publication_id = 11;
.. such a mistake simply cannot happen.