MySql Join left me with an empty table - mysql

I made a query that list all the players on all the teams for each of the clubs in my site.
A "club" has "teams" and each team has "players", I solve the listing using this query:
SELECT
Club.*,
Teams.*,
Players.*
FROM
(Club LEFT JOIN Teams ON Club.idClub = Teams.idClub)
LEFT JOIN
Teams ON Teams.idEquipos_Categorias = Players.idEquipos_Categorias
WHERE
Teams.idTemporadas = 2017
It works great, now I want to list just the players than "has played" from other table "hasPlay" I made this changes, but It returns empty:
SELECT
Club.*,
Teams.*,
Players.*,
HasPlay.*
FROM
((Club LEFT JOIN Teams ON Club.idClub = Teams.idClub)
LEFT JOIN
Teams ON Teams.idEquipos_Categorias = Players.idEquipos_Categorias) LEFT JOIN HasPLay ON Players.idPlayer = HasPlay.idPlayer
WHERE
Teams.idTemporadas = 2017
I think I mess it up in the last join...
Any advice is well recieved

I doubt your original query works
as it contains various errors and anomalies
anyway you should avoid useless ()
and especially use JOIN on the Equipos_Categorias table eg:
SELECT
Club.*,
Teams.*,
Players.*,
HasPlay.*
FROM Club LEFT JOIN Teams ON Club.idClub = Teams.idClub
LEFT JOIN Teams ON Teams.idEquipos_Categorias = Players.idEquipos_Categorias
LEFT JOIN HasPLay ON Players.idPlayer = HasPlay.idPlayer
INNER JOIN Equipos_Categorias ON Teams.idEquipos_Categorias = Equipos_Categorias.idEquipos_Categorias
and Equipos_Categorias.idTemporadas = 2017
ORDER BY Club.nombre, Equipos_Categorias.idEquipos_Categorias ASC,
Jugadores_Equipos.categoriaJugador ASC, Jugadores_Equipos.nombre ASC
and as verified by OP uisng team table
SELECT
Club.*,
Teams.*,
Players.*,
HasPlay.*
FROM Club LEFT JOIN Teams ON Club.idClub = Teams.idClub
INNER JOIN Teams ON Teams.idEquipos_Categorias = Players.idEquipos_Categorias and team.idTemporadas = 2017
LEFT JOIN HasPLay ON Players.idPlayer = HasPlay.idPlayer
ORDER BY Club.nombre, Equipos_Categorias.idEquipos_Categorias ASC,
Jugadores_Equipos.categoriaJugador ASC, Jugadores_Equipos.nombre ASC

Related

MYSQL Inner Join with IF Statement

I'm trying to join several tables in my database.
I need to get account information from the 'accounts' table with the latest meter history on it.
And if an account has no meter history, I want it to show 'meter' related fields as NULL.
Here's my query so far:
SELECT
accounts.id,
accounts.account_order,
acc.id AS accounts_class_id,
acc.zone,
acc.book,
acc.service_class,
acc.size,
acc.account_no AS series_no,
accounts.status,
application_address.address_line,
concessionaires.firstname,
concessionaires.middlename,
concessionaires.lastname,
mb.brand_name,
m.meter_no,
ms.meter_status
FROM
accounts
INNER JOIN
applications
ON accounts.application_id = applications.id
LEFT JOIN
application_address
ON applications.application_no = application_address.application_no
LEFT JOIN
concessionaires
ON applications.concessionaire_no = concessionaires.concessionaire_no
INNER JOIN
accounts_classifications acc
ON accounts.id = acc.account
INNER JOIN meter_history mh
ON mh.id = (SELECT id FROM meter_history mh2
WHERE mh2.account_id = accounts.id
ORDER BY mh2.status_date DESC
LIMIT 1)
LEFT JOIN
meter_status ms
ON mh.meter_status = ms.id
INNER JOIN
meter m
ON mh.meter = m.id
LEFT JOIN
meter_brand mb
ON m.meter_brand = mb.id
WHERE
acc.book = 1 AND acc.zone = 20 AND applications.status = '6' AND acc.status = '1'
This would return only accounts with meter history on it.
Where should I put my IF condition so I get accounts with no history as well, or if that is even possible with my query. Thank you!

MYSQL Merge two columns from two tables and still use LEFT JOIN

So I'm having a slight problem with having to save price on a product in two different tables due to a few reasons. Is it possible to merge two columns into one? I know UNION exists but does it work with LEFT JOIN's?
Any pointers is much appreciated.
Best Regards
SELECT
si.id AS shop_item_id,
si.item_price,
s.logo_file_name,
p.cat_id AS category_id,
api.item_price AS api_price,
MAX(c.campaign_desc) AS campaignDesc,
MAX(c.campaign_type_id) AS campaignType,
MAX(c.shop_id) AS campaign_shop_id,
MAX(ct.image_name) AS campaignLogo
FROM
shop_item si
LEFT JOIN
shop s ON
s.id = si.shop_id
LEFT JOIN
product p ON
si.product_id = p.id
LEFT JOIN
campaign_category cc ON
cc.category_id = p.cat_id
LEFT JOIN
campaign c ON
c.id = cc.campaign_id AND
c.shop_id = si.shop_id AND
c.show_in_pricetable = 1 AND
NOW() BETWEEN c.date_from and c.date_to
LEFT JOIN
campaign_type ct ON
c.campaign_type_id = ct.id
LEFT JOIN
shop_api_item api ON
si.rel_feed_api = api.unique_id AND si.shop_id = api.shop_id
WHERE
si.`product_id` = 586 AND
s.`active_shop` = 1
GROUP BY
s.name,
si.id ,
si.item_price
ORDER BY
si.`item_price`,
si.`shop_id`,
c.`campaign_desc` DESC
It looks like you would benefit from the COALESCE() function.
SELECT
si.id AS shop_item_id,
COALESCE(si.item_price, api.item_price) AS coalesced_price,
...
COALESCE() takes multiple arguments, and returns the first argument that is not NULL.

mysql query join statement

I have a problem in a query.
i need to make a page where user can see all reservations he made with movie name, cinema name, seats code that he reserved
i reached this level
SELECT member.member_username, show_datetime, movie_name
FROM `member`
JOIN `reservation` ON `reservation`.`member_id` = `member`.`member_id`
JOIN `show` ON `show`.`show_id`= `reservation`.`show_id`
JOIN `movie` ON `movie`.`movie_id` = `show`.`movie_id`
WHERE `reservation`.`member_id`=1
i need to make a connection and get also the cinema_name from cinema table and seats_code from seat table and theater name
in fact, i need a query to give me all almost all data in my whole database
here is the schema for the DB
http://imageshack.us/photo/my-images/824/58054944.jpg/
JOIN these two tables too:
SELECT
m.member_username,
sh.show_datetime,
v.movie_name,
c.cinema_name,
t.theater_name
FROM member AS m
INNER JOIN reservation AS r ON r.member_id = m.member_id
INNEr JOIN show AS sh ON sh.show_id = r.show_id
INNER JOIN movie AS v ON v.movie_id = s.movie_id
INNER JOIN theater AS t ON t.theater_id = sh.theater_id
INNER JOIN cinema AS c ON c.theater_id = sh.theater_id
WHERE r.member_id = 1
Keep joining your tables
SELECT member.member_username, show_datetime, movie_name, c.cinema_name, t.theater_name
FROM `member`
JOIN `reservation` ON `reservation`.`member_id` = `member`.`member_id`
JOIN `show` ON `show`.`show_id`= `reservation`.`show_id`
JOIN `movie` ON `movie`.`movie_id` = `show`.`movie_id`
JOIN `theater` ON `show`.`theater_id` = `theater`.`theater_id`
JOIN `cinema` ON `theater`.`cinema_id` = `cinema`.`cinema_id`
JOIN `seat` ON `show`.`theater_id` = `seat`.`theater_id`
WHERE `reservation`.`member_id`=1

Joining 6 MySQL tables for sports application

I'm creating a database that is going to be used for a sporting competition. To view the results of a game, I need to join six tables together:
Season s
Round r
Rounddetails rd
Match m
Matchdetails md
Game g
I want the select statement to return something like this:
g.gameid, g.pointsfor, g.pointsagainst, md.matchdetailsid, m.matchid, rd.rounddetailsid, r.roundid, s.seasonid
Could anyone help me write a MySQL statement that will return that?
Please click the link to view the tables
EDIT: so far I have tried this query:
select
`s`.`seasonID` AS `seasonID`,
`r`.`roundID` AS `roundid`,
`rd`.`roundDetailsID` AS `roundDetailsID`,
`m`.`matchid` AS `matchid`,
`md`.`matchDetailsID` AS `matchDetailsID`,
`g`.`gameid` AS `gameid`,
`g`.`pointsfor` AS `pointsfor`,
`g`.`pointsagainst` AS `pointsagainst`
from (((((`season` `s` left join `round` `r` on((`s`.`SeasonID` = `r`.`SeasonID`)))
left join `rounddetails` `rd` on((`r`.`RoundID` = `rd`.`RoundID`)))
left join `match` `m` on((`rd`.`matchid` = `m`.`matchid`)))
left join `matchdetails` `md` on((`m`.`matchid` = `md`.`matchid`)))
left join `game` `g` on((`md`.`matchdetailsid` = `g`.`matchdetailsid`)))
SELECT DISTINCT g.gameid, g.pointsfor, g.pointsagainst, md.matchdetailsid, m.matchid, rd.rounddetailsid, r.roundid, s.seasonid
FROM Match AS m
INNER JOIN MatchDetails as md
ON Match.MatchId = MatchDetails.MatchId
INNER JOIN Game as g
ON MatchDetails.MatchDetailSid = MatchDetailSid
INNER JOIN RoundDetails as rd
ON Match.MatchId = RoundDetails.MatchId
INNER JOIN Round as r
ON Round.RoundId = RoundDetails.RoundId
INNER JOIN Season as s
ON Round.SeasonId = Season.SeasonId
WHERE MatchId = 1

mysql join and count

Somehow am not successful with creating the query that I want.
DB is to do with locations, there are the following tables which are relevant
t_location - list of locations incl. field t_location_zipcode, and t_location_id_location
t_zipcodecity - join table just t_zipcodecity_zipcode t_zipcodecity_id_city
t_city - city list with t_city_id_city
t_citystate - join table, t_citystate_id_city, t_citystate_id_state
t_state - list of states with t_state_id_state
Initially I tried to get a list of states with locations using this query:
SELECT DISTINCT `t_state_id_state`
, `t_state_name_full`
FROM (`t_state`)
LEFT JOIN `t_citystate` ON `t_state_id_state` = `t_citystate_id_state`
LEFT JOIN `t_city` ON `t_citystate_id_state` = `t_city_id_city`
LEFT JOIN `t_zipcodecity` ON `t_city_id_city` = `t_zipcodecity_id_city`
LEFT JOIN `t_location` ON `t_zipcodecity_zipcode` = `t_location_zipcode`
ORDER BY `t_state_name_full` asc ­
which works fine.
Now what I also need / want which I am failing dismally at is to get the number of locations in each state. I don't know if it can be done in this one query or if i need another, either way I need help!
you can use a count and a group by. Something like this:
SELECT DISTINCT `t_state_id_state`
, `t_state_name_full`
, COUNT(*)
FROM (`t_state`)
LEFT JOIN `t_citystate` ON `t_state_id_state` = `t_citystate_id_state`
LEFT JOIN `t_city` ON `t_citystate_id_state` = `t_city_id_city`
LEFT JOIN `t_zipcodecity` ON `t_city_id_city` = `t_zipcodecity_id_city`
LEFT JOIN `t_location` ON `t_zipcodecity_zipcode` = `t_location_zipcode`
GROUP BY `t_state_id_state` , `t_state_name_full`
ORDER BY `t_state_name_full` asc
SELECT t_state_id_state
, t_state_name_full
, COUNT(DISTINCT t_location_id_location) AS locations_number
FROM
t_state
LEFT JOIN
t_citystate ON t_state_id_state = t_citystate_id_state
LEFT JOIN
t_city ON t_citystate_id_state = t_city_id_city
LEFT JOIN
t_zipcodecity ON t_city_id_city = t_zipcodecity_id_city
LEFT JOIN
t_location ON t_zipcodecity_zipcode = t_location_zipcode
GROUP BY t_state_id_state
ORDER BY t_state_name_full ASC ­
Ok thats looking good, my bad with the left joins, i was initially trying to get ALL the states which is why i used them. But i change that to use inner joins
SELECT t_state_id_state,
t_state_name_full,
COUNT(DISTINCT t_location_id_location) AS locations_number
FROM t_state
INNER JOIN t_citystate ON t_citystate_id_state = t_state_id_state
INNER JOIN t_city ON t_city_id_city = t_citystate_id_city
INNER JOIN t_zipcodecity ON t_zipcodecity_id_city = t_city_id_city
INNER JOIN t_location ON t_location_zipcode = t_zipcodecity_zipcode
GROUP BY t_state_id_state
ORDER BY t_state_name_full ASC
then i actually end up with a result that looks good !