UNION SELECT tables with inner join and GET values with "ALIAS" - php

So I have 2 tables, Matches and Teams, what I want to do is get some values from the match and Inner join "Teams" to get the names of both teams and add them to a php array later on (getting it all in one sql)
Matches
- IDMatch
- IDLocalTeam
- IDVisitorTeam
- Time
- Half
- Stopped
Teams
- IDTeam
- name
What I have by now is
$query = "SELECT * FROM `Matches`
INNER JOIN `Teams` ON `Matches`.IDLocalTeam = `Teams`.IDTeam
UNION SELECT * FROM `Matches` INNER JOIN `Teams`
ON `Matches`.IDVisitorTeam = `Teams`.IDTeam
ORDER BY IDMatch DESC;";
If someone could help me it would be great! Thanks alot

Wouldn't it be easier to use INNER JOIN twice? It's definitely quicker way of doing joins anyway.
SELECT
*
FROM
`Matches` m
INNER JOIN `Teams` t1
ON m.IDLocalTeam = t1.IDTeam
INNER JOIN `Teams` t2
ON m.IDVisitorTeam = t2.IDTeam
ORDER BY
m.IDMatch DESC;
Also start using aliases instead of table names to identify fields in query, it will SQL much smaller.

Related

Joining the same table twice with different values

I'm trying to replace the value of "transfers.pickup_areas_group_id" and "transfers.drop_areas_group_id" with the values from the table "areas_group", using IDs
I'm using this query:
SELECT
transfers.id AS transfer_id,
transfers.name AS transfer_name,
transfers.pickup_areas_group_id AS transfer_pickup_areas_group_id,
transfers.drop_areas_group_id AS transfer_drop_areas_group_id,
transfers_pricing.vehicle_id AS vehicle_id,
transfers_pricing.date_start AS date_start,
transfers_pricing.date_end AS date_end,
transfers_pricing.price AS price
FROM transfers
INNER JOIN transfers_pricing ON transfers_pricing.transfer_id = transfers.id
I tried an extra INNER JOIN to replace the first value "transfers.pickup_areas_group_id", but I couldn't find a way to replace the second one "transfers.drop_areas_group_id"
I tried this query:
SELECT
transfers.id AS transfer_id,
transfers.name AS transfer_name,
transfers.pickup_areas_group_id AS transfer_pickup_areas_group_id,
areas_group.area_id AS pickup_area_ids,
transfers.drop_areas_group_id AS transfer_drop_areas_group_id,
transfers_pricing.vehicle_id AS vehicle_id,
transfers_pricing.date_start AS date_start,
transfers_pricing.date_end AS date_end,
transfers_pricing.price AS price
FROM transfers
INNER JOIN transfers_pricing ON transfers_pricing.transfer_id = transfers.id
INNER JOIN areas_group ON areas_group.id = transfers.pickup_areas_group_id
Thank you,
Basically, you need another join on areas_group; to disambiguate the two references to the same table, you need to use a table alias.
Actually, it is a good practice to use table aliases for all tables that come into play in the query: this makes the query shorter to read and write.
SELECT
t.id AS transfer_id,
t.name AS transfer_name,
t.pickup_areas_group_id AS transfer_pickup_areas_group_id,
ag1.area_id AS pickup_area_ids,
t.drop_areas_group_id AS transfer_drop_areas_group_id,
ag2.area_id AS drop_area_ids
tp.vehicle_id AS vehicle_id,
tp.date_start AS date_start,
tp.date_end AS date_end,
tp.price AS price
FROM transfers t
INNER JOIN transfers_pricing tp ON tp.transfer_id = t.id
INNER JOIN areas_group ag1 ON ag1.id = t.pickup_areas_group_id
INNER JOIN areas_group ag2 ON ag2.id = t.drop_areas_group_id

Combine two mysql group queries

I'm making a web app to create tournaments and as i have learned PHP in the course of this project, so my skills aren't probably the best.
I have an identifier in my database day2_semifinal or day2_additional which basically identifies the type of semifinal.
So my first query is:
$numberquery = mysql_query("
SELECT *
FROM tourneyplayers
INNER JOIN results
on (resultid=r_id)
INNER JOIN players
ON (p_id=playerid)
INNER JOIN tourneys
on (T_Id=tourneyid)
WHERE tourneyid='$tourneyid' and
in_day2 = 1 and
day2_semifinal IS NOT NULL
GROUP BY day2_semifinal
ORDER BY agegroupid",$connection);
This will get me all the semifinal groups, i'll iterate over them and query all the players in group:
$semigroup = $group['day2_semifinal'];
$playerQuery = mysql_query("
SELECT *
FROM tourneyplayers
INNER JOIN results
on (r_id=resultid)
INNER JOIN players
on (p_id=playerid)
WHERE tourneyid='$tourneyid' AND
day2_semifinal = '$semigroup' and
in_day2 = 1
ORDER BY day2startplace",$connection);
Now after i've created tables and echoed all the data from player queries for day2_semifinal, i run another query:
$numberquery = mysql_query("SELECT * FROM tourneyplayers INNER JOIN results on (resultid=r_id) INNER JOIN players ON (p_id=playerid) WHERE tourneyid='$tourneyid' and in_day2 = 1 and day2_additional_nosemi IS NOT NULL AND day2_additional_nosemi <> 0 GROUP BY day2_additional_nosemi ORDER BY agegroupid",$connection);
Which is fairly similar to the first one, only thing different is day2_semifinal identifiers have changed to day2_additional. After that query, i'll again, iterate over the day2_additional_nosemi groups and query the players inside of them:
$additionalgroup = $group['day2_additional_nosemi'];
$playerQuery = mysql_query("SELECT * FROM tourneyplayers INNER JOIN results on (r_id=resultid) INNER JOIN players on (p_id=playerid) WHERE tourneyid='$tourneyid' AND day2_additional_nosemi = '$additionalgroup' and in_day2 = 1 ORDER BY day2startplace",$connection);
Now this works, but this creates an issue with ordering, since the first query orders them by agegroupid but only for players in day2_semifinal (and i'd like to have day2_additional players ordered together with day2_semifinal). If i run another query the previous data has already been echoed and ordering is not right. How could i concatenate two $numberquery queries in order to select players after them as well?
I'm answering my own question as i figured out a way to do this. What i did, was removed ORDER BYfrom both queries and created a new query which concatenated the two with UNION:
SELECT * FROM (
SELECT *
FROM tourneyplayers as tp1
INNER JOIN results as r1
on (tp1.resultid=r1.r_id)
INNER JOIN players as p1
ON (p1.p_id=tp1.playerid)
WHERE tp1.tourneyid=96 and
tp1.in_day2 = 1 and
r1.day2_semifinal IS NOT NULL
GROUP BY r1.day2_semifinal
UNION ALL
SELECT *
FROM tourneyplayers as tp2
INNER JOIN results as r2
on (tp2.resultid=r2.r_id)
INNER JOIN players as p2
ON (p2.p_id=tp2.playerid)
WHERE tp2.tourneyid=96 and
tp2.in_day2 = 1 and
r2.day2_additional_nosemi IS NOT NULL AND
r2.day2_additional_nosemi <> 0
GROUP BY r2.day2_additional_nosemi
) t ORDER BY t.agegroupid;

MySQL INNER JOIN query issue

I have created an INNER JOIN query as shown below and was wondering how I can make it work? I need for the HomeTeam and AwayTeam to equal TeamID in the query. Any help would be much appreciated. Thanks
$result = mysqli_query($con,"SELECT results.*,team.TeamName
FROM results
INNER JOIN team ON team.TeamID = results.HomeTeam
INNER JOIN team on team.TeamID = results.AwayTeam");
You need to use aliases for the table that you are including twice. Otherwise mysql cannot distinguish between the two.
To be able to process the results easily, you can do the same with the names you are selecting.
Something like:
SELECT
results.*,
t1.TeamName AS TeamNameHome,
t2.TeamName AS TeamNameAway
FROM results
INNER JOIN team t1
ON t1.TeamID = results.HomeTeam
INNER JOIN team t2
ON t2.TeamID = results.AwayTeam

MySQL Using SUM with multiple joins

I have a projects table and a tasks table I want to do a query that gets all projects and the sum of the time_spent columns grouped by project id. So essentially list all projects and get the total of all the time_spent columns in the tasks table belonging to that project.
With the query posted below I get the latest added time_spent column and not the sum of all the columns.. :S
Below is the query I have at the moment:
SELECT `projects`.`id`, `projects`.`description`, `projects`.`created`,
`users`.`title`, `users`.`firstname`, `users`.`lastname`, `users2`.`title`
as assignee_title, `users2`.`firstname` as assignee_firstname,
`users2`.`lastname` as assignee_lastname,
(select sum(tasks2.time_spent)
from tasks tasks2
where tasks2.id = tasks.id)
as project_duration
FROM (`projects`)
LEFT JOIN `users`
ON `users`.`id` = `projects`.`user_id`
LEFT JOIN `users` as users2
ON `users2`.`id` = `projects`.`assignee_id`
LEFT JOIN `tasks` ON `tasks`.`project_id` = `projects`.`id`
GROUP BY `projects`.`id`
ORDER BY `projects`.`created` DESC
Below is my projects table:
Below is my tasks table:
Thanks in advance!
Usually this query will help you.
SELECT p.*, (SELECT SUM(t.time_spent) FROM tasks as t WHERE t.project_id = p.id) as project_fulltime FROM projects as p
In your question, you don't say about users. Do you need users?
You are on right way, maybe your JOINs can't fetch all data.
This query should do it for you.
Note, whenever you do a group by you must include every column that you select from or order by. Some MySql installations don't prevent you from doing this, but in the end it results in an incorrect result set.
As well you should never do a query as part of your SELECT statement, known as a sub-query, as it will result in an equal amount of additional queries in relation to the number of rows returned. So if you got 1,000 rows back, it would result in 1,001 queries instead of 1 query.
SELECT
p.id,
p.description,
p.created,
u.title,
u.firstname,
u.lastname,
a.title assignee_title,
a.firstname assignee_firstname,
a.lastname assignee_lastname,
SUM(t.time_spent) project_duration
FROM
projects p
LEFT JOIN
users u ON
u.id = p.user_id
LEFT JOIN
users a ON
a.id = u.assignee_id
LEFT JOIN
tasks t ON
t.project_id = p.id
GROUP BY
p.id,
p.description,
p.created,
u.title,
u.firstname,
u.lastname,
a.title,
a.firstname,
a.lastname
ORDER BY
p.created DESC

SQL /php LEFT JOIN - Only display one instance

I am using the following query which works great but I need a modification to it.
SELECT *
FROM `table1` LEFT JOIN
`table2` ON table1.messageid=table2.messageid
WHERE `venue_active` = 1
My problem here is that I table2.messageid might have more than 1 match and I only what 1 displayed, but I still want all the records on table1 to be returned.
For example:
table1.messageid might have lots of children on Table2 and I only need 1 displayed
How can I do this?
UPDATE: I've tried adding: GROUP BY table1.messageid but it returns nothing.
add a group by clause
SELECT *
FROM `table1` LEFT JOIN
`table2` ON table1.messageid=table2.messageid
WHERE `venue_active` = 1
GROUP BY table1.messageid
just add group by
SELECT *
FROM `table1` LEFT JOIN
`table2` ON table1.messageid=table2.messageid
WHERE `venue_active` = 1
GROUP BY table1.messageid
I don't have your exact tables but this is the equivilent of your query running off a couple of my tables which are 1:many relationships tblusers being your table1 and tblmessages being your table2
SELECT tblusers.ID FROM `tblusers` LEFT JOIN tblmessages ON tblusers.ID=tblmessages.recipient_user_id WHERE tblmessages.state=1
This returns this
If I now change the query to be this
SELECT DISTINCT tblusers.ID FROM `tblusers` LEFT JOIN tblmessages ON tblusers.ID=tblmessages.recipient_user_id WHERE tblmessages.state=1
I now get a unique list of all users which have at least one message in my messages table.

Categories