MySQL SELECT two records from other table? - mysql

NOTE: This is not the same as this question, as I need to get data from two other records, not two fields from one one other record!
MySQL newb. I have two tables, and I want to get data from both of them so I have the following:
wp_bowling_fixtures
fixture_id | fixture_date | home_team_id | away_team_id
-----------+--------------+--------------+-------------
1 | 2017-12-12 | 1 | 2
2 | 2017-12-12 | 3 | 4
3 | 2017-12-12 | 5 | 6
4 | 2017-12-12 | 7 | 8
5 | 2017-12-12 | 9 | 10
wp_bowling_teams
team_id | name | division | archived
--------+--------+----------+---------
1 | Team A | 1 | 0
2 | Team B | 1 | 0
3 | Team C | 2 | 1
4 | Team D | 2 | 0
5 | Team E | 3 | 0
6 | Team F | 3 | 0
7 | Team G | 4 | 0
8 | Team H | 4 | 1
9 | Team I | 4 | 0
10 | Team J | 4 | 0
The result I want a SELECT query to produce:
fixture_id | fixture_date | home_team_id | home_team_name | home_team_archived | home_team_division | away_team_id | away_team_name | away_team_archived | away_team_division
-----------+--------------+--------------+----------------+--------------------+--------------------+--------------+----------------+--------------------+-------------------
1 | 2017-12-12 | 1 | Team A | 0 | 1 | 2 | Team B | 0 | 1
I also want it ordered by fixture_date DESC, home_team_division ASC, home_team_name ASC.
Hope that makes sense.
TIA,
Nick.

SELECT f.fixture_id, f.fixture_date, h.team_id as home_team_id, h.name as home_team_name, h.archived as home_team_archived, h.division as home_team_division, a.team_id as away_team_id, a.name as away_team_name, a.archived as away_team_archived, a.division as away_team_division FROM wp_bowling_fixtures f, wp_bowling_teams h, wp_bowling_teams a where f.home_team_id = h.team_id and f.away_team_id = a.team_id order by f.fixture_date desc, h.division asc, h.name asc;
Works.

Nothing in sql stops you joining a table twice but if you do so you must alias them .
You're looking for a query that uses
fixtures f
JOIN teams h ON f.home_team_id = h.team_id
JOIN teams a ON f.away_team_id = a.team_id
I gave the teams h (for home) and a (for away) aliases so I could tell them apart. Give it a go at filling it out

select x.fixture_id, x.fixture_date, x.home_team_id, x.home_team_name, x.home_team_archived, x.home_team_division,
y.away_team_id, y.away_team_name, y.away_team_archived, y.away_team_division from
(select a.fixture_id, a.fixture_date, a.home_team_id, b.name home_team_name, b.archived home_team_archived, b.division home_team_division
from wp_bowling_fixtures a inner join wp_bowling_teams b on A.home_team_id = B.team_id) x
inner join
(select a.fixture_id, a.fixture_date, a.away_team_id, b.name away_team_name, b.archived away_team_archived, b.division away_team_division
from wp_bowling_fixtures a inner join wp_bowling_teams b on A.away_team_id = B.team_id) y on x.fixture_id = y.fixture_id
order by x.fixture_date desc, home_team_division asc, home_team_name asc;`enter code here`

Related

Order multiple rows by field from most recent entry

I'm trying to return a list of users, ordered by a field (pt_seen) on the users most recent row.
Does that make sense?
So far I have:
SELECT u.users_id,
u.username,
ed.date
FROM users u
LEFT JOIN exercises_done ed
ON ed.user_id = u.users_id
WHERE u.pt_id = 1
GROUP BY u.users_id
Which obviously just returns the users grouped.
The tables look like this:
users:
users_id | pt_id | username
1 | 1 | billy
2 | 1 | bob
3 | 1 | sue
exercises_done:
exercises_done_id | user_id | pt_id | exercises_done_date | pt_seen
1 | 1 | 1 | 2018-01-01 | 0
2 | 1 | 1 | 2018-01-02 | 0
3 | 1 | 1 | 2018-01-03 | 1
4 | 2 | 1 | 2018-01-05 | 1
5 | 3 | 1 | 2018-01-04 | 0
and I'm trying to get results like this:
users_id | username | exercises_done_date | pt_seen
1 | billy | 2018-01-02 | 0
3 | sue | 2018-01-04 | 0
2 | bob | 2018-01-05 | 1
The aim is that I show users at the top of the list who have a pt_seen value of 0, then ordered by exercises_done_date.
Any help would be great,
You can select the most recent exercise in the where clause rather than using aggregation:
SELECT u.users_id, u.username, ed.*
FROM users u LEFT JOIN
exercises_done ed
ON ed.user_id = u.users_id
WHERE ed.exercises_done_date = (SELECT MAX(ed2.exercises_done_date)
FROM exercises_done ed2
WHERE ed2.user_id = ed.user_id
) AND
u.pt_id = 1
ORDER BY u.pt_seen, exercises_done_date DESC;
You can add an ORDER BY clause after your GROUP BY.
SELECT u.users_id, u.username, ed.date FROM
users u
LEFT JOIN exercises_done ed ON ed.user_id = u.users_id
WHERE
u.pt_id = 1
GROUP BY
u.users_id
ORDER BY
pt_seen, exercises_done_date

Using left join with min

I am trying to connect two tables with left join and a date.
My SQL Query
SELECT
ord.`ordernumber` bestellnummer,
his.`change_date` zahldatum
FROM
`s_order` ord
LEFT JOIN
`s_order_history` his ON ((ord.`id`=his.`orderID`) AND (ord.`cleared`=his.`payment_status_id`)) #AND MIN(his.`change_date`)
WHERE
ord.`ordertime` >= \''.$dateSTART.'\' AND ord.`ordertime` <= \''.$dateSTOP.'\'' ;
s_order
+----+---------------------+---------+-------------+
| id | ordertime | cleared | ordernumber |
+----+---------------------+---------+-------------+
| 1 | 2014-08-11 19:53:43 | 2 | 123 |
| 2 | 2014-08-15 18:33:34 | 2 | 125 |
+----+---------------------+---------+-------------+
s_order_history
+----+-------------------+-----------------+---------+---------------------+
| id | payment_status_id | order_status_id | orderID | orderID change_date |
+----+-------------------+-----------------+---------+---------------------+
| 1 | 1 | 5 | 1 | 2014-08-11 20:53:43 |
| 2 | 2 | 5 | 1 | 2014-08-11 22:53:43 |
| 3 | 2 | 7 | 1 | 2014-08-12 19:53:43 |
| 4 | 1 | 5 | 2 | 2014-08-15 18:33:34 |
| 5 | 1 | 6 | 2 | 2014-08-16 18:33:34 |
| 6 | 2 | 6 | 2 | 2014-08-17 18:33:34 |
+----+-------------------+-----------------+---------+---------------------+
Wanted result:
+-------------+---------------------+
| ordernumber | change_date |
+-------------+---------------------+
| 123 | 2014-08-11 22:53:43 |
| 125 | 2014-08-17 18:33:34 |
+-------------+---------------------+
The problem I have is getting only the date, where the cleared/payment_status_id value has been changed in s_order. I currently get all dates where the payment_status_id matches the current cleared value, but I only need the one, where it happend first.
This is only an excerpt of the actually query, since the original is a lot longer (mostly more left joins and a lot more tables).
You can group data by ordernumber
SELECT
ord.`ordernumber` bestellnummer,
MIN(his.`min_change_date`) as zahldatum
FROM
`s_order` ord
LEFT JOIN
`s_order_history` his ON ((ord.`id`=his.`orderID`) AND (ord.`cleared`=his.`payment_status_id`)) #AND MIN(his.`change_date`)
WHERE
ord.`ordertime` >= \''.$dateSTART.'\' AND ord.`ordertime` <= \''.$dateSTOP.'\''
GROUP BY
ord.`ordernumber`;
or you can group data in a subquery:
SELECT
ord.`ordernumber` bestellnummer,
his.`min_change_date` zahldatum
FROM
`s_order` ord
LEFT JOIN (
SELECT
orderID, payment_status_id, MIN(change_date) as min_change_date
FROM
s_order_history
GROUP BY
orderID, payment_status_id
) his ON (ord.`id` = his.`orderID` AND ord.`cleared` = his.`payment_status_id`)
WHERE
ord.`ordertime` >= \''.$dateSTART.'\' AND ord.`ordertime` <= \''.$dateSTOP.'\'';
Try this:
select s_order.ordernumber, min(s_order_history.change_date)
from s_order left join s_order_history
on s_order.id = s_order_history.orderID
and s_order.cleared = s_order_history.payment_status_id
group by s_order.order_id
SELECT ord.`ordernumber` bestellnummer,
MIN( his.`change_date` ) zahldatum
...
GROUP BY ord.`ordernumber`
MIN is an aggregate function so you can't use it in a JOIN straight up like you've tried above. You also are not comparing it to a value in your JOIN.
You'll want to do something like:
his.`change_date` = (SELECT MIN(his.`change_date`) FROM s_order_history where ord.`id` = his.`orderID`)
in your JOIN.

Joining tables but needs 0 for empty rows

I don't know how to explain the scenario using words. So am writing the examples:
I have a table named tblType:
type_id | type_name
---------------------
1 | abb
2 | cda
3 | edg
4 | hij
5 | klm
And I have another table named tblRequest:
req_id | type_id | user_id | duration
-------------------------------------------
1 | 4 | 1002 | 20
2 | 1 | 1002 | 60
3 | 5 | 1008 | 60
....
So what am trying to do is, fetch the SUM() of duration for each type, for a particular user.
This is what I tried:
SELECT
SUM(r.`duration`) AS `duration`,
t.`type_id`,
t.`type_name`
FROM `tblRequest` AS r
LEFT JOIN `tblType` AS t ON r.`type_id` = t.`type_id`
WHERE r.`user_id` = '1002'
GROUP BY r.`type_id`
It might return something like this:
type_id | type_name | duration
-------------------------------
1 | abb | 60
4 | hij | 20
It works. But the issue is, I want to get 0 as value for other types that doesn't have a row in tblRequest. I mean I want the output to be like this:
type_id | type_name | duration
-------------------------------
1 | abb | 60
2 | cda | 0
3 | edg | 0
4 | hij | 20
5 | klm | 0
I mean it should get the rows of all types, but 0 as value for those type that doesn't have a row in tblRequest
You could perform the aggregation on tblRequest and only then join it, using a left join to handle missing rows and coalesce to convert the nulls to 0s:
SELECT t.type_id, type_name, COALESCE(sum_duration, 0) AS duration
FROM tblType t
LEFT JOIN (SELECT type_id, SUM(duration) AS sum_duration
FROM tblRequest
WHERE user_id = '1002'
GROUP BY type_id) r ON t.type_id = r.type_id
Select a.type_id, isnull(sum(b.duration), 0)
From tblType a Left Outer Join tblRequest b
ON a.type_id = b.type_id and b.user_id = 1002
Group by a.type_id

Grouping from highest to lowest

I have a table like this:
+---------+--------------+---------+
| visitty | specialty | doctors |
+---------+--------------+---------+
| 1 | oncology | 3611 |
| 1 | neurology | 1931 |
| 1 | rheumatology | 1471 |
| 0 | oncology | 35 |
| 0 | rheumatology | 28 |
| 0 | neurology | 20 |
+---------+--------------+---------+
The above table was created by ordering the field doctors
Now, I'm trying to get the following result:
+---------+--------------+---------+
| visitty | specialty | doctors |
+---------+--------------+---------+
| 1 | oncology | 3611 |
| 0 | oncology | 35 |
| 1 | neurology | 1931 |
| 0 | neurology | 20 |
| 1 | rheumatology | 1471 |
| 0 | rheumatology | 28 |
+---------+--------------+---------+
Is there any way to do this?
In reply to Adil Miedl ... these were the criteria used in the query:
(PS: I think it can be a little confusing to underestand without the referenced tables)
SELECT
su.visitty, cs.specialty, COUNT(*) doctors
FROM
contacts c
INNER JOIN contact_groups ccgc ON c.id_contact = ccgc.id_contact
AND ccgc.status = 1
INNER JOIN groups ccg ON ccg.id_ccenter_groups = ccgc.id_ccenter_groups
AND ccg.status = 2
INNER JOIN distribuition ccd ON ccd.id_ccenter_groups = ccg.id_ccenter_groups
AND ccd.status = 2
INNER JOIN cds_contacts sc ON c.id_cds_account = sc.id_cds_account
LEFT JOIN cds_contacts_territories AS sct ON sc.id_contact = sct.id_contact
INNER JOIN cds_usuarios_territories AS sut ON sut.id_territory = sct.id_territory
INNER JOIN cds_usuarios AS su ON su.id_user = sut.id_user
INNER JOIN contact_specialties cs ON c.id_contact = cs.id_contact
and cs.status = 1
and cs.srsmain = 'Y'
WHERE
c.contact_type = 'Doctor'
AND ccd.release_date BETWEEN '2013-01-01 00:00:00' AND '2013-12-31 23:59:59'
GROUP by visitty , cs.specialty
ORDER BY doctors DESC;
You seem to want to order the specialties by the total of doctors in them. The query needs to calculate this value before doing the ordering:
select t.visitty, t.specialty, t.doctors
from table t join
(select specialty, sum(doctors) as numdoctors
from table t
group by specialty
) tsum
on t.specialty = tsum.specialty
order by tsum.doctors desc, tsum.specialty, t.doctors desc;

Querying specific data from 3 tables

I'm having trouble coming up with a query that returns the player's id, name along with the player's first match date, matchid and opponent.
I want the same information for player's last match as well.
`players`
id | name
1 | playername10
2 | playername22
3 | playername33
4 | playername45
5 | playername55
`matches`
id | gamedate | opponent
1 | 2011-01-01 | opponent1
2 | 2011-01-02 | opponent2
3 | 2011-01-03 | opponent3
4 | 2011-01-04 | opponent4
5 | 2011-01-05 | opponent5
`playermatchscores`
id | matchid | player | goals
1 | 1 | playername10 | 1
2 | 1 | playername22 | 2
3 | 2 | playername10 | 1
4 | 1 | playername33 | 1
5 | 3 | playername45 | 2
6 | 4 | playername55 | 1
7 | 2 | playername55 | 1
8 | 3 | playername22 | 2
9 | 5 | playername55 | 1
Where matchid is a foreign key to the id in table matches.
I tried several queries but I may be approaching it the the wrong way. How can I write a way to get the information I want?
Information about LEFT JOIN: http://www.w3schools.com/sql/sql_join_left.asp
SELECT players.id, MAX(matches.gamedate) AS first_match, MIN(matches.gamedate) AS last_match
FROM playermatchscores
LEFT JOIN players ON players.player = playermatchscores.player
LEFT JOIN matches ON matches.id = playermatchscores.matchid
GROUP BY players.player
I haven't tested this select.
P.S. You should use foreign key for players table too with player_id in playermatchscores.
After the changes in question:
SELECT players.*, matches.*,
FROM playermatchscores
LEFT JOIN players ON players.name = playermatchscores.player
LEFT JOIN matches ON matches.id = playermatchscores.matchid
ORDER BY matches.gamedate ASC
WHERE players.id = 3
LIMIT 1
For the last match replace ASC with DESC.
P.S. This is not the best way to do it but it should work.