I wanted to add "ID_CHOOSE2", "ID_CHOOSE3" etc to each user, what kind of query would I have to create for it to be correct? I Tried use "OR":
ON w.ID_CHOOSE OR w.ID_CHOOSE2 = m.ID_PERSON AND m.ID_CHOOSE OR m.ID_CHOOSE2 = w.ID_PERSON
And
ON (w.ID_CHOOSE = m.ID_PERSON AND m.ID_CHOOSE = w.ID_PERSON)
OR (w.ID_CHOOSE2 = m.ID_PERSON AND m.ID_CHOOSE2 = w.ID_PERSON)
As you can see, the query is not working well because it is poorly structured.
I mean a comparison of the type: A person chose some person (choose) and another (choose2). Check if the other person chose the first person in all their choices (choose, choose2 etc). If so, they match, if not, do not display and check others people. I want output like:
TOM W | KATE B
MARK K | KATE B
MARK K | ALEX S
My code with desc: https://onecompiler.com/mysql/3xz4552uv
-- EDIT FOR MORE INFO ---
I have a table with men and women (users)
And two other tables containing the choices of men and women
Now I would like to find a match (who matches whom based on choices in "Choose" columns - they are not always in sequence. Sometimes it's 3,2,1 and sometimes it's 1,3,2)
This is illustrated in the example below:
ID_PERSON MAN:
1 - JACK - Choose: 1,3,2 (Mary, Emily, Lilly)
2 - TOM - Choose: 1,2,3 (Mary, Lilly, Emily)
3 - HARRY - Choose: 2,1 (Lilly, Mary)
| ID | ID_PERSON | ID_CHOOSE | ID_CHOOSE2 | ID_CHOOSE3 |
| 1 | 1 | 1 | 3 | 2 |
| ID | ID_PERSON | ID_CHOOSE | ID_CHOOSE2 | ID_CHOOSE3 |
| 2 | 2 | 1 | 2 | 3 |
| ID | ID_PERSON | ID_CHOOSE | ID_CHOOSE2 | ID_CHOOSE3 |
| 3 | 3 | 2 | 1 | NULL(empty) |
ID_PERSON WOMAN:
1 - Mary - Choose: 3,1,2 (Harry, Tom, Jack)
2 - Lilly - Choose: 1,2 (Jack, Harry)
3 - Emily - Choose: 1,2,3 (Jack, Tom, Harry)
| ID | ID_PERSON | ID_CHOOSE | ID_CHOOSE2 | ID_CHOOSE3 |
| 1 | 1 | 3 | 1 | 2 |
| ID | ID_PERSON | ID_CHOOSE | ID_CHOOSE2 | ID_CHOOSE3 |
| 2 | 2 | 1 | NULL(empty) | 2 |
| ID | ID_PERSON | ID_CHOOSE | ID_CHOOSE2 | ID_CHOOSE3 |
| 3 | 3 | 1 | 2 | 3 |
Match:
JACK Matched with: Mary, Emily, Lilly
TOM Matched with: Mary, Emily
Harry Matched with: Lilly, Harry
Thanks
Here is an alternative schéma.
See dbFiddle link below.
SELECT * FROM users;
SELECT * FROM choices;
id | name | gender
--: | :------- | :-----
101 | KATE B | F
102 | ALEX S | F
203 | TOM W | M
204 | MARK K | M
205 | DENNIS A | M
chid | chooser | choice
---: | ------: | -----:
1 | 203 | 101
2 | 203 | 102
3 | 204 | 102
4 | 204 | 101
5 | 101 | 203
6 | 101 | 204
7 | 102 | 204
8 | 102 | 205
select
m.id, m.name man,
f.id, f.name woman
from
choices c
join choices s
on c.chooser = s.choice
left join users m on m.id = c.chooser
left join users f on f.id = c.choice
where s.chooser = c.choice
and m.gender = 'M'
and f.gender = 'F';
id | man | id | woman
--: | :----- | --: | :-----
203 | TOM W | 101 | KATE B
204 | MARK K | 101 | KATE B
204 | MARK K | 102 | ALEX S
db<>fiddle here
Related
Let say I have 2 tables:
courses
| id | name | is_sort_by_borrower |
|----|--------------|---------------------|
| 1 | Comp Science | 1 |
| 2 | Biz Admin | 0 |
| 3 | Physics | 1 |
books
| id | title | borrower | last_borrow_date | course_id |
| --- | ------ | -------- | ---------------- | --------- |
| 1 | Book A | 523 | 11/12/19 12:33 | 1 |
| 2 | Book B | 423 | 11/15/19 12:33 | 1 |
| 3 | Book C | 401 | 10/22/19 12:33 | 1 |
There's a column is_sort_by_borrower in courses, I want to get a result like this
| course | book_title | borrower | last_borrow_date | is_sort_by_borrower |
|--------------|------------|----------|------------------|---------------------|
| Comp Science | Book A | 523 | 11/12/19 12:33 | 1 |
| Comp Science | Book B | 423 | 11/15/19 12:33 | 1 |
| Comp Science | Book C | 401 | 10/22/19 12:33 | 1 |
| Comp Science | Book D | 377 | 11/19/19 12:33 | 1 |
| Biz Admin | Book E | 122 | 11/20/19 12:33 | 0 |
| Biz Admin | Book F | 419 | 11/12/19 12:33 | 0 |
| Biz Admin | Book G | 65 | 10/18/19 12:33 | 0 |
| Physics | Book H | 446 | 8/31/19 12:33 | 1 |
| Physics | Book I | 398 | 11/2/19 12:33 | 1 |
| Physics | Book J | 376 | 9/30/19 12:33 | 1 |
| Physics | Book K | 249 | 10/11/19 12:33 | 1 |
Is it possible to sort the result some by borrower, some by last_borrow_date?
Yes, create another column in the SELECT and use CASE to populate it with the value you want, then sort by that. You can also use the CASE statement in the WHERE clause, depending on if you want the sorting column to show the result or not.
ref: MYSQL ORDER BY CASE Issue
ref: https://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html
In your example:
SELECT
c.id, c.name, c.is_sort_by_borrower,
b.id, b.title, b.borrower, b.last_borrow_date
FROM
courses c
INNER JOIN
books b on b.course_id = c.id
ORDER BY
CASE
WHEN is_sort_by_borrower=1 THEN borrower
WHEN is_sort_by_borrower=0 THEN last_borrow_date
END ASC
The ORDER BY needs to look like:
ORDER BY course -- courses grouped together
, CASE WHEN is_sort_by_borrower = 1 THEN borrower END DESC
, CASE WHEN is_sort_by_borrower = 0 THEN last_borrow_date END DESC
Exactly one of the two columns will be NULL for a given course depending on the sort flag. You must not combine the two datatypes in same column otherwise result will be unpredictable.
I have these three tables and I want to get the sum of the price field of table 3 linked to table 1, table 2 being linked to table 1 and 3:
Table 1 - Tecnic
+--------------+------------+
| id_tecnic | name |
+--------------+------------+
| 1 | Michael |
| 2 | Billy |
| 3 | Joe |
+--------------+------------+
Table 2 - Linked
+--------------+------------+------------+
| id_linked | id_tecnic | id_mer |
+--------------+------------+------------+
| 1 | 1 | 4 |
| 2 | 3 | 1 |
| 3 | 3 | 2 |
+--------------+------------+------------+
Table 3 - Mer
+--------------+------------+
| id_mer | price |
+--------------+------------+
| 1 | 30 |
| 2 | 70 |
| 3 | 50 |
| 4 | 10 |
+--------------+------------+
Result
+--------------+------------+-------------+
| id_tecnic | name | total_price |
+--------------+------------+-------------+
| 1 | Michael | 10 |
| 2 | Billy | 0 |
| 3 | Joe | 90 |
+--------------+------------+-------------+
I have tried this:
SELECT te.id_tecnic,
SUM(m.price)
FROM tecnic te
LEFT JOIN linked l ON te.id_tecnic = l.id_tecnic
LEFT JOIN mer m ON l.id_mer = m.id_mer
GROUP BY te.id_tecnic
Billy doesn't have record in Table 2 - Linked so with purpose to have Billy in result set you have to start from table Table 3 - Mer, like:
SELECT te.id_tecnic, te.name, SUM(m.price)
FROM mer m
LEFT jOIN linked l ON l.id_mer = m.id_mer
LEFT JOIN te ON te.id_tecnic = l.id_tecnic
GROUP BY te.id_tecnic;
I need a help of a sql expert. I have a problem with my sql query. I have six MySQL tables:
pcs_persons (table of players)
+----+------------+------------+
| id | firstname | lastname |
+----+------------+------------+
| 1 | John | McClane |
| 2 | Jack | Marriott |
| 3 | Billy | Bravo |
| 4 | Archie | MacDonald |
+----+------------+------------+
pcs_matchs (table of match results)
+----+-------------------+-------------------+---------+------------+------------+
| id | id_candidate_dom | id_candidate_ext | id_day | id_season | id_compet |
+----+-------------------+-------------------+---------+------------+------------+
| 1 | 1 | 2 | 1 | 1 | 1 |
| 2 | 3 | 4 | 1 | 1 | 1 |
| 3 | 2 | 3 | 2 | 1 | 1 |
| 4 | 4 | 1 | 2 | 1 | 1 |
| 5 | 1 | 7 | 1 | 2 | 3 |
| 6 | 6 | 3 | 2 | 2 | 5 |
+----+-------------------+-------------------+---------+------------+------------+
pcs_lineup (table of those players who were selected to the match squad as starter - type 2, or as substitute - type 3)
+----+-----------+----------+------------+-------+
| id | id_match | id_club | id_person | type |
+----+-----------+----------+------------+-------+
| 1 | 1 | 1 | 1 | 2 |
| 2 | 1 | 1 | 2 | 3 |
| 3 | 1 | 2 | 3 | 2 |
| 4 | 1 | 2 | 4 | 3 |
+----+-----------+----------+------------+-------+
pcs_goals (table of scored goals by players)
| id | id_match | id_person | id_club | goal_min |
+----+-----------+------------+----------+-----------+
| 1 | 1 | 1 | 1 | 23 |
| 2 | 1 | 1 | 1 | 48 |
| 3 | 1 | 3 | 2 | 56 |
| 4 | 1 | 4 | 2 | 89 |
+----+-----------+------------+----------+-----------+
pcs_cards (table of received cards by players)
| id | id_match | id_person | id_club | card_min | card_yellow | card_red |
+----+-----------+------------+----------+-----------+--------------+-----------+
| 1 | 1 | 1 | 1 | 12 | 1 | |
| 2 | 1 | 1 | 1 | 43 | 1 | |
| 3 | 1 | 3 | 2 | 78 | | 1 |
| 4 | 1 | 4 | 2 | 91 | 1 | |
+----+-----------+------------+----------+-----------+--------------+-----------+
pcs_subs (table of substitutions)
| id | id_match | id_club | id_person_in | id_person_out | subs_min |
+----+-----------+----------+---------------+----------------+-----------+
| 1 | 1 | 1 | 7 | 1 | 82 |
| 2 | 1 | 1 | 8 | 2 | 85 |
| 3 | 1 | 2 | 5 | 3 | 89 |
| 4 | 1 | 2 | 6 | 4 | 91 |
+----+-----------+----------+---------------+----------------+-----------+
My current query is here:
SELECT pcs_lineup.id_person, pcs_lineup.id_club, pcs_lineup.type,
pcs_persons.lastname, pcs_persons.firstname, count( pcs_lineup.id_person) AS apps, count(pcs_subs.id_person_in) AS subs
FROM pcs_lineup
JOIN pcs_matchs ON pcs_matchs.id = pcs_lineup.id_match
JOIN pcs_persons ON pcs_persons.id = pcs_lineup.id_person
LEFT JOIN pcs_subs ON pcs_subs.id_person_in = pcs_lineup.id_person
WHERE pcs_lineup.id_club =2
AND pcs_matchs.id_compet =1
AND pcs_matchs.id_season =1
AND pcs_lineup.type = 2 OR pcs_subs.id_person_in IS NOT NULL AND pcs_subs.id_club =2
GROUP BY id_person
My current result structure (list of players who played as member of starting lineup or played as substitutes, players who just sat on the bench not counted)
+-----------+----------+-------+-----------+------------+-------+-------+
| id_person | id_club | type | lastname | firstname | apps | subs |
+-----------+----------+-------+-----------+------------+-------+-------+
I would like to add extra columns (goals, yellow cards, red cards) to the result, but I don't know how.
The structure of desired result:
+-----------+----------+-------+-----------+------------+-------+----------+-------+--------+---------------+------------+
| id_person | id_club | type | lastname | firstname | apps | starter | subs | goals | yellow cards | red_cards |
+-----------+----------+-------+-----------+------------+-------+----------+-------+--------+---------------+------------+
I hope that some expert could help for me, because I have no idea how could I join these tables for the desired result. Many thanks!
Modified code (results are not good)
SELECT pcs_lineup.id_person
,pcs_lineup.id_club
,pcs_lineup.type
,pcs_persons.lastname
,pcs_persons.firstname
,count( pcs_lineup.id_person) AS apps
,CASE WHEN pcs_lineup.type = 2 THEN 'YES' END starter
,count(pcs_subs.id_person_in) AS subs
,count(pcs_goals.goal_min) AS goals
,count(card_yellow) as "Yellow Cards"
,count(card_red) as "Red Card"
FROM pcs_lineup
JOIN pcs_matchs ON pcs_matchs.id = pcs_lineup.id_match
JOIN pcs_persons ON pcs_persons.id = pcs_lineup.id_person
LEFT JOIN pcs_subs ON pcs_subs.id_person_in = pcs_lineup.id_person
LEFT JOIN pcs_goals ON pcs_goals.id_match = pcs_matchs.id
AND pcs_persons.id = pcs_goals.id_person
LEFT JOIN pcs_cards ON pcs_cards.id_match = pcs_matchs.id
AND pcs_cards.id_person = pcs_persons.id
AND pcs_goals.id_club = pcs_cards.id_club
WHERE pcs_lineup.id_club =2
AND pcs_matchs.id_compet =1
AND pcs_matchs.id_season =1
AND pcs_subs.id_person_in IS NOT NULL AND pcs_subs.id_club =2
GROUP BY id_person
You need to join 2 more tables i.e. pcs_goals and pcs_cards -
SELECT pcs_lineup.id_person
,pcs_lineup.id_club
,pcs_lineup.type
,pcs_persons.lastname
,pcs_persons.firstname
,count( pcs_lineup.id_person) AS apps
,CASE WHEN pcs_lineup.type = 2 THEN 'YES' END starter
,count(pcs_subs.id_person_in) AS subs
,count(pcs_goals.goals_min) AS goals
,count(card_yellow) as "Yellow Cards"
,count(card_red) as "Red Card"
FROM pcs_lineup
JOIN pcs_matchs ON pcs_matchs.id = pcs_lineup.id_match
JOIN pcs_persons ON pcs_persons.id = pcs_lineup.id_person
LEFT JOIN pcs_subs ON pcs_subs.id_person_in = pcs_lineup.id_person
LEFT JOIN pcs_goals ON pcs_goals.id_match = pcs_matchs.id
AND pcs_persons.id = pcs_matchs.id_person
LEFT JOIN pcs_cards ON pcs_cards.id_match = pcs_matchs.id
AND pcs_cards.id_person = pcs_persons.id
AND pcs_goals.id_club = pcs_cards.id_club
WHERE pcs_lineup.id_club =2
AND pcs_matchs.id_compet =1
AND pcs_matchs.id_season =1
AND pcs_subs.id_person_in IS NOT NULL AND pcs_subs.id_club =2
GROUP BY id_person
I am not sure what do you mean by starter column.
pcs_subs.id_person_in change to id_person_out
So, I have 3 tables :
guest :
id_guest | name
1 | John
2 | Nick
3 | James
4 | Paul
5 | Chris
6 | Karen
7 | Peter
room :
id_room | status
1 | Clean
2 | Dirty
3 | Dirty
4 | Clean
5 | Clean
6 | Clean
reservation :
id_guest | id_room | date
1 | 1 | 2015-04-15
1 | 1 | 2015-04-16
1 | 1 | 2015-04-17
2 | 3 | 2015-04-15
3 | 4 | 2015-04-15
3 | 4 | 2015-04-16
4 | 2 | 2015-04-16
5 | 2 | 2015-04-17
6 | 2 | 2015-04-18
And this is what the expected output should be :
id_room | status | d04-15 | d04-16 | d04-17 | d04-18
1 | Clean | John | John | John |
2 | Dirty | | Paul | Chris | Karen
3 | Dirty | Nick | | |
4 | Clean | James | James | |
5 | Clean | | | |
6 | Clean | | | |
I have been able to show it until the third field (d04-15) though with the date as values, using :
SELECT room.id_room,
room.status,
reservation.date AS d04-15
FROM room
LEFT JOIN reservation
ON room.id_room = reservation.id_room AND reservation.date = '2015-04-15'
GROUP BY room.id_room
But I'm not sure as to how to display the name there and
appending new fields (d04-16, d04-17, and d04-18) from another JOIN statement.
Any help would be appreciated. Thanks.
The columns returned in a query can't be altered at run time; they have to be statically declared in the SQL SELECT statement.
Here's an example of a statement that can achieve the specified result:
SELECT m.id_room
, m.status
, MAX(IF(r.date='2015-04-15',g.name,NULL)) AS `d04-15`
, MAX(IF(r.date='2015-04-16',g.name,NULL)) AS `d04-16`
, MAX(IF(r.date='2015-04-17',g.name,NULL)) AS `d04-17`
, MAX(IF(r.date='2015-04-18',g.name,NULL)) AS `d04-18`
FROM room m
LEFT
JOIN reservation r
ON r.id_room = m.id_room
AND r.date IN ('2015-04-15','2015-04-16','2015-04-17','2015-04-18')
LEFT
JOIN guest g
ON g.id_guest = r.id_guest
GROUP BY m.id_room
structure:
tbl 1
|car_id(PK)| make | model | year |
-----------------------------------
| 1 | Toyot | Camry | 1999 |
| 2 | Honda | Civic | 2005 |
tbl 2
|img_id(PK)| car_id| img_link |
------------------------------------
| 1 | 1 | tcamry1.jpeg |
| 2 | 1 | tcamry2.jpeg |
| 3 | 1 | tcamry3.jpeg |
| 4 | 2 | hcivic1.jpeg |
| 5 | 2 | hcivic2.jpeg |
My query:
SELECT *
FROM cars c
LEFT JOIN imgs g
ON c.car_id=g.car_id
WHERE 1
Result:
|img_id(PK)| car_id| make | model | year | img_link |
-----------------------------------------------------------
| 1 | 1 | Toyot | Camry | 1999 | tcamry1.jpeg |
| 2 | 1 | Toyot | Camry | 1999 | tcamry2.jpeg |
| 3 | 1 | Toyot | Camry | 1999 | tcamry3.jpeg |
| 4 | 2 | Honda | Civic | 2005 | hcivic1.jpeg |
| 5 | 2 | Honda | Civic | 2005 | hcivic2.jpeg |
I need to get 1 row for each car and have WHERE clause with something like lowest img_id value out of all img_id related to the same car.
Result I want:
|img_id(PK)| car_id| make | model | year | img_link |
-----------------------------------------------------------
| 1 | 1 | Toyot | Camry | 1999 | tcamry1.jpeg |
| 4 | 2 | Honda | Civic | 2005 | hcivic1.jpeg |
Thank you.
UPDATE:
I need something along these lines :-/
SELECT g.id, c.car_id, c.mc_make, c.mc_model, c.mc_year, c.mc_desc
FROM mycars c
INNER JOIN (SELECT * FROM mycars_gallery g WHERE )
ON c.car_id=g.car_id
WHERE g.id = min(g.id)
Try:
SELECT MIN(b.img_id), a.car_id, a.make, a.model, a.year, b.img_link
FROM cars a
LEFT JOIN imgs b ON a.car_id = b.car_id
GROUP BY a.car_id, a.make, a.model, a.year ;
Demo: http://sqlfiddle.com/#!2/1469f/15
Hope this helps.
SELECT *
FROM cars c
LEFT JOIN imgs g
ON c.car_id=g.car_id
WHERE img_id IN(SELECT MIN(img_id)) GROUP BY model
SELECT *
FROM cars c
LEFT JOIN imgs g
ON c.car_id=g.car_id
WHERE 1
GROUP BY g.img_link;
try this.. not sure though.