Select a table and join 2 different tables - mysql

I have 3 table, book table is primary table, female and male are secondary, I need join second and third by first by conditional.
example book:
+------+---------+----------+
| b_id | b_title | b_author |
+------+---------+----------+
| 1 | First | 3 |
| 2 | Second | 1 |
| 3 | Third | -4 |
| 4 | test | -3 |
+------+---------+----------+
male:
+------+--------+
| m_id | m_name |
+------+--------+
| 1 | John |
| 2 | Jim |
| 3 | Jessy |
| 4 | Mike |
| 5 | Tom |
+------+--------+
female:
+------+--------+
| f_id | f_name |
+------+--------+
| 1 | Sarah |
| 2 | Shanon |
| 3 | Nina |
| 4 | Shina |
| 5 | Mary |
+------+--------+
Now I need select from book and when the b_author is positive select from male table and where the b_author select from female table.
SELECT b_id,b_title, IF(b_author > 0,m_name,f_name) AS 'b_author' FROM book -- how make join here.

select *
from book b
left join male m on m.m_id=b.b_author
left join female f on f.f_id=b.b_author*-1

Related

Mysql 2 select staments with value from statement 1 used in statement 2

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.

SELECT record NOT IN SET of Record

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

How to select all rows which have identical values

Here is my table:
+----+-------+------+
| id | name | code |
+----+-------+------+
| 1 | jack | 1 |
| 2 | peter | 1 |
| 3 | jack | 1 |
| 4 | ali | 2 |
| 5 | peter | 3 |
| 6 | peter | 1 |
| 7 | ali | 2 |
| 8 | jack | 3 |
| 9 | peter | 2 |
| 10 | peter | 4 |
+----+-------+------+
I want to select all rows that satisfy: the number of {those rows which its code value is between 1-3 and its name vale is identical} be more or equal than 4
From the above data, I want this output:
+----+-------+------+
| id | name | code |
+----+-------+------+
| 2 | peter | 1 |
| 5 | peter | 3 |
| 6 | peter | 1 |
| 9 | peter | 2 |
+----+-------+------+
How can I do that?
Use a subquery to figure out which names should be returned, then build your main query on that.
Try this:
select *
from mytable
where name in (
select name
from mytable
where code between 1 and 3
group by name
having count(*) > 3)
and code between 1 and 3

SQL Select rows with max value from joined table

I have these 3 tables like that:
lecturers:
+-------------+---------+
| id-lecturer | name |
+-------------+---------+
| 1 | Johnson |
| 2 | Smith |
| ... | ... |
| ... | ... |
+-------------+---------+
subjects:
+------------+---------+
| id-subject | name |
+------------+---------+
| 1 | Math |
| 2 | Physics |
| ... | ... |
| ... | ... |
+------------+---------+
exams:
+---------+-------------+------------+------------+
| id-exam | id-lecturer | id-subject | date |
+---------+-------------+------------+------------+
| 1 | 5 | 1 | 1990-05-05 |
| 2 | 7 | 1 | ... |
| 3 | 5 | 3 | ... |
| ... | ... | ... | ... |
+---------+-------------+------------+------------+
When i try to do the first SELECT:
SELECT e.`id-lecturer`, e.`id-subject`, COUNT(e.`id-lecturer`) AS `exams-num`
FROM exams e
JOIN subjects s ON e.`id-subject`=s.`id-subject`
JOIN lecturers l ON e.`id-lecturer`=l.`id-lecturer`
GROUP BY e.`id-lecturer`, e.`id-subject`
I get the right answer. It shows something like that:
+-------------+------------+-----------+
| id-lecturer | id-subject | exams-num |
+-------------+------------+-----------+
| 0001 | 1 | 4 |
| 0001 | 3 | 1 |
| 0001 | 4 | 1 |
| 0001 | 5 | 1 |
| 0002 | 1 | 2 |
| 0002 | 2 | 1 |
| 0002 | 4 | 1 |
| 0002 | 6 | 3 |
+-------------+------------+-----------+
Now i want to show only the max number for every lecturer, my code is:
SELECT it.`id-lecturer`, it.`id-subject`, MAX(it.`exams-num`) AS `exams-number`
FROM (
SELECT e.`id-lecturer`, e.`id-subject`, COUNT(e.`id-lecturer`) AS `exams-num`
FROM egzaminy e
JOIN subjects s ON e.`id-subject`=s.`id-subject`
JOIN lecturers l ON e.`id-lecturer`=l.`id-lecturer`
GROUP BY e.`id-lecturer`, e.`id-subject`) it
GROUP BY it.`id-lecturer`
output:
+-------------+------------+--------------+
| id-lecturer | id-subject | exams-number |
+-------------+------------+--------------+
| 0001 | 1 | 4 |
| 0002 | 1 | 3 |
| 0003 | 1 | 2 |
| 0004 | 1 | 5 |
| 0005 | 2 | 1 |
+-------------+------------+--------------+
I get the correct numbers of the max values for each lecturer, but the subjects id doesn't match, it always takes the first row's id. How can I make it to match correctly these two fields in every row?
I guess you can simply use the same query for further conditions like below.
Select t.Lecturer_id,max(t.exams-num) from
(SELECT e.id-lecturer as Lecturer_id, e.id-subject as Subject_id,
COUNT(e.id-lecturer) AS exams-num
FROM exams e
JOIN subjects s ON e.id-subject=s.id-subject
JOIN lecturers l ON e.id-lecturer=l.id-lecturer
GROUP BY e.id-lecturer, e.id-subject ) as t
group by t.Lecturer_id

MYSQL query: All records where user=jack from table `B` must be returned with the corresponding color matching `photo_id`

Here are two overly simplified version of 2 tables I'm using:
A:
+-------+-----------------------+
| id | photo_id | color |
+-------+-----------------------+
| 1 | 100 | red |
| 2 | 101 | blue |
| 3 | 102 | green |
+-------+-----------------------+
B:
+-------+-----------------------+
| id | photo_id | user |
+-------+-----------------------+
| 1 | 100 | jack |
| 2 | 101 | jill |
| 3 | 102 | jack |
| 4 | 103 | jill |
| 5 | 104 | jack |
| 6 | 105 | jack |
| 7 | 106 | jack |
+-------+-----------------------+
This is the query I'm running right now:
SELECT * FROM B WHERE user='jack';
But, now I need to get the following result
C:
+-------+-----------------------+--------+
| id | photo_id | user | color |
+-------+-----------------------+--------+
| 1 | 100 | jack | red |
| 2 | 102 | jack | blue |
| 3 | 104 | jack | green |
| 4 | 105 | jack | |
| 5 | 106 | jack | |
+-------+-----------------------+--------+
All records where user=jack from table B must be returned with the corresponding color matching photo_id.
How can this be done?
Untested, but here goes:
SELECT * FROM B LEFT JOIN A ON A.photo_id = B.photo_id WHERE user = 'jack'
select * from B LEFT JOIN A using(photo_id) where B.user = 'jack';