Hello guys this my first Q in stackoverflow so i'll be clear with you i'm very new to php so take it easy on me .
right so what am trying to do is i have 5 tables where's the relation have already been set
and i'm trying to show the related categorys and platforms using the game id note that the category has a table on it own and so as the platform then there's two other tables which have the game id and the cat id together and same as for the platform and the game and the field i have in the games table are:
id-->for the game id
name-->for the name of the game
details and image.
and in the game_cat:
g_id and cat_id
then thers the table for the category which has the name and id
and the same for the platform . these are my tables which i'm trying to select from
enter image description here
and my sql is:
SELECT games.*,
game_cat.*,
category.*
FROM games,
game_cat,
category
WHERE games.id='game_cat.g_id'
AND game_cat.g_id='game_cat.cat_id'
AND game_cat.cat_id='category.id'
but it doesn't work on phpmyadmin sql so I've done some research and there's something called join in sql which i'm not familiar with.
any help is appreciated.
Don't use single quotes for column name (when need use eventually backtics)
Use inner join if you have alway columns match (otherwise you left join ) and you can use alias for a compact query
(i have added also the last two tables ...hope the related columns name are right)
SELECT g.*,gc.*,c.* , gp.*, p.*
FROM games g
INNER JOIN game_cat gc on g.id = gc.g_id
INNER JOIN category c on gc.cat_id=c.id
INNER JOIN game_platform gp on g.id = gp.g_id
INNER JOIN platform p on gp.paltform_id=p.id
You need to JOIN between the tables based on the foreign-key relationship between them (if you don't know what this is, go read up on it!). Something like this (I don't know what columns represent the foreign keys, so making this up):
SELECT games.*,
game_cat.*,
category.*
FROM games g
INNER JOIN game_cat gc on (g.game_id = gc.g_id)
INNER JOIN category c on (gc.cat_id = c.cat_id)
WHERE games.id='game_cat.g_id'
AND game_cat.g_id='game_cat.cat_id'
AND game_cat.cat_id='category.id';
This looks like a standard implementation of a many-to-many relationship between games and category - would that be correct?
Related
So, I wish to join two tables (reservation and facility_items). The reservation.facility_item_id is composed of multiple facility_item_id in a single row. And now I am having trouble joining them because I can't connect the reservation.facility_item_id to facility_items.facility_item_id
Here is the structure of my tables:
reservation
facility_item
reservation content
facility_item content
`SELECT * FROM reservations r
JOIN facility_items f on r.facility_item_id IN
(
SELECT facility_item_id
FROM facility_items
)`
lmao. I do not even know what I am doing but thats my start.
Expected result should show 3 facility_item_id but instead, I got this:
Wow, I really didn't check the content on the tables, it is indeed more complicated than a simple join. I guess you are complicating things because this a 1xN relation and you are storing it on the wrong side (the 1 side instead of the N side).
You should have a field in your facility table which shoud store the ID of the reservation. Then a simple join will suffice:
SELECT *
FROM reservations r JOIN facility f
ON r.reservation_id = f.reservation_id
First attempt, Won't work
It seems that a simple join should suffice. Can you try this one?
SELECT *
FROM reservations r JOIN facility_items f
ON r.facility_item_id = f.facility_item_id
I’ve got quite a few tables with product information. The columns on each table that I’m pulling from in this particular query have the exact same column names. I’ve been attempting to do it via a UNION ALL but for some reason it is throwing an error saying non-object but all the column names are correct.
I’m using a format that I found online. But obviously something is wrong. There are more tables; however, this is how it starts (with 2). I’d prefer not to have to code each select statement in the union with unique table abbreviations if I don’t have to.
I don’t have to use union if there is a better method.
All tables share data on Product_Categories and Product_Sub_Category.
The only thing unique to each table is id and part_number.
SELECT f.id,f.part_number,f.cat,f.subcat,f.table_name FROM
(
SELECT t.id,t.part_number,psc.name as subcat,c.name as cat, c.table_name FROM Steel_Strapping as t JOIN Product_Sub_Category as psc ON t.subcat = psc.id JOIN Product_Categories as c ON psc.category = c.id ORDER BY c.sort_order,psc.sort_order,t.sort_order
UNION ALL
SELECT t.id,t.part_number,psc.name as subcat,c.name as cat, c.table_name FROM Product as t JOIN Product_Sub_Category as psc ON t.subcat = psc.id JOIN Product_Categories as c ON psc.category = c.id ORDER BY c.sort_order,psc.sort_order,t.sort_order
) f
My end result is one full list of all products sharing column names. Ex: $result[‘part_number’] will pull part numbers from ALL tables listed in union.
I found the solution when playing around with code. I had to add parenthesis (select...) UNION JOIN (select...) inside the parent select statement
I have a simple database with few tables (and some sample columns):
game (gm_id , game_name , company , desc)
plateform_master (p_id , plateform_name)
plateform_details (pd_id, gm_id, p_id, release_date)
Is there a way to create single SQL query which will return all game details with multiple plateform
Your question is not very clear. To which posts and categories are you referring yourself?
Here an example based on what I understood: Select all records from plateform_master and, for each of them, get its details from plateform_details. You can uncomment the where part in order to get one master record with its details. ("--" means comment in SQL)
select
pm.p_id
pd.*
from plateform_master as pm
inner join plateform_details as pd on pm.p_id = pd.p_id
-- where pm.p_id = 123
Sorry, my bad, it should be left join, not inner join!
With your changed requirements try this (not tested): Select all games and, for each one, get it's plateform_details and plateform_master correspondent values:
select
gm.gm_id,
gm.game_name,
gm.company,
pd.release_date,
pm.plateform_name
from game as gm
left join plateform_details as pd on pd.gm_id = gm.gm_id
left join plateform_master as pm on (pm.p_id = pd.p_id AND pd.gm_id = gm.gm_id)
-- WHERE CONDITIONS
;
Thanks for voting. I also wanted to tell you, that you have no many-to-many relationships here. You have just one-to-many. Many-to-many means the use of a middle table between two other ones.
How can I get all Groups, by the Person ID in this mysql model? I know I need a join colunm or some Hibernate/JPA black magic, but I don't know how to do this.
Here is the model I'm using in study.
Link with image if is not been displayed: http://i.imgur.com/pbCkIVX.png
To reduce space here are the entities:
Github Repository
The following MySQL query will retrieve all groups for a given idPerson
SELECT g.*
FROM `Group` g
JOIN PersonOnGroup pog on g.idGroup = pog.idGroup
WHERE pog.idPerson = myPersonId
I don't know what your hibernate entities look like but something along these lines should work
from Group as group
inner join group.persons as person
where person.idPerson = 1
I have a query that requires what I think is a complicated JOIN. I have three tables that are sort of "children" of each other. The top table is "clan_members". The next is "roster_members" which gets the clan_member id. The bottom one is "match_players" which gets the roster_members id. I wrote a loop that takes me through all of the results in the clan_members table. What I want to do is find out how many matches that clan member has played in. Here's the layouts of the three tables:
[clan_members]
-id
- member_id
-join_date
[roster_members]
- id
- clan_member_id
- title
[match_players]
- id
- roster_member_id
- match_id
I have never done a JOIN with three different tables before and I have no idea what order to do them in. I would greatly appreciate it if someone could write me a query!
This query will get you the number of matches that clan member with id 123 has participated in:
select count(*) as match_count
from clan_members c, roster_members r, match_players m
where c.member_id = r.clan_member_id
and r.id = m.roster_member_id
and c.id = 123
On a side note, it would be good practice to name your columns consistently. For example, all columns that have the clan_member_id should be named the same. In the clan_members table its called id but in the roster_members table its called clan_member_id. Just makes it easier to understand how the tables join together.
SELECT DISTINCT match_id
FROM clan_members
INNER JOIN roster_members
ON clan_members.id = roster_members.clan_member_id
INNER JOIN match_players
ON roster_members.id = match_players.roster_member_id;
will get you the # of unique match_ids when all three tables are joined.