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.
Related
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?
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 am using the following JOIN statement:
SELECT *
FROM students2014
JOIN notes2014 ON (students2014.Student = notes2014.NoteStudent)
WHERE students2014.Consultant='$Consultant'
ORDER BY students2014.LastName
to retrieve a list of students (students2014) and corresponding notes for each student stored in (notes2014).
Each student has multiple notes within the notes2014 table and each note has an ID that corresponds with each student's unique ID. The above statement is returning a the list of students but duplicating every student that has more than one note. I only want to display the latest note for each student (which is determined by the highest note ID).
Is this possible?
You need another join based on the MAX noteId you got from your select.
Something like this should do it (not tested; next time I'd recommed you to paste a link to http://sqlfiddle.com/ with your table structure and some sample data.
SELECT *
FROM students s
LEFT JOIN (
SELECT MAX(NoteId) max_id, NoteStudent
FROM notes
GROUP BY NoteStudent
) aux ON aux.NoteStudent = s.Student
LEFT JOIN notes n2 ON aux.max_id = n2.NoteId
If I may say so, the fact that a table is called students2014 is a big code smell. You'd be much better off with a students table and a year field, for many reasons (just a couple: you won't need to change your DB structure every year, querying across years is much, much easier, etc, etc). Perhaps you "inherited" this, but I thought I'd mention it.
GROUP the query by studentId and select the MAX of the noteId
Try :
SELECT
students2014.Student,
IFNULL(MAX(NoteId),0)
FROM students2014
LEFT JOIN notes2014 ON (students2014.Student = notes2014.NoteStudent)
WHERE students2014.Consultant='$Consultant'
GROUP BY students2014.Student
ORDER BY students2014.LastName
I want to get some data out of my database that is similar to a receipt you get at the supermarket (just an example which suits kinda good to my real situation).
For example you get the 2 (always only 2) products 1 and 3. These products are stored in a seperated product database.
Your shopping result is stored in one database containing all the details like time, location and so on AND in 2 columns (product_1, and product_2). Again this shopping situation is only a comparison to my real situation so I know that this would not be a good database structure for a shopping list.
So now I would like to get the Whole receipt but instead of printing the product IDs I would like to have the Name and for example the price on it.
If I had only one product I would use this query:
SELECT `list`.`time`, `list`.`location`, `prod`.`prod_name`, `prod`.`prod_price`
FROM `shopping_list` `list`, `products` `prod`
WHERE `list`.`product_1` = `prod`.`prod_id`
But since I have two products I cannot just go on with
AND `list`.`product_2` = `prod`.`prod_id`
But how do you achive what I would like to have?
Thank you very much,
Phil
You'll need to join to the product table twice
e.g.
SELECT
`list`.`time`,
`list`.`location`,
`prod1`.`prod_name` `prod_name1`,
`prod1`.`prod_price` `prod_price1` ,
`prod2`.`prod_name` `prod_name2`,
`prod2`.`prod_price` `prod_price2`
FROM `shopping_list` `list`
INNER JOIN `products` `prod1`
ON `list`.`product_1` = `prod1`.`prod_id`
INNER JOIN `products` `prod2`
ON `list`.`product_2` = `prod2`.`prod_id`
I'm not sure what your business rules are so you may need to convert the second INNER JOIN to a LEFT JOIN if they need to always select two products.
i don't know your exact situation but usually field names like product_1 and product_2 indicate bad database design. however if you really need that you need to join the products table twice.
select
*
from
list l
, product p1
, product p2
where
l.product_1 = p1.product_id
and l.product_2 = p2.product_id
(this is oracle syntax but i think it will work also in mysql).
hth.
The previous answers work well if you can parse out the two lines from 1 database result row. If you want two lines from the database you could do a UNION query:
SELECT `list`.`time`, `list`.`location`, `prod`.`prod_name`, `prod`.`prod_price`
FROM `shopping_list` `list`, `products` `prod`
WHERE `list`.`product_1` = `prod`.`prod_id`
UNION
SELECT `list`.`time`, `list`.`location`, `prod`.`prod_name`, `prod`.`prod_price`
FROM `shopping_list` `list`, `products` `prod`
WHERE `list`.`product_2` = `prod`.`prod_id`
I created an inventory system which mostly is using a server-sided scripting language to do all the work. To try and get some performance gains I am looking to better design my database to try and minimizing the scripts.
I have a table named metal_part which has a one to one relationship with five other tables, basically the other tables are other parts, which those parts then have a one to one relationship with a few other tables.
When I query metal_part I need all the UPC numbers from each table, so its direct one to one relationships need to get their own information from their direct one to one relationship tables ect... Is it possible to make a huge query to build it all and put it in a form at like:
(###) - ####/##/##/## [a-z]
Using a query? or do I have to get all the information and concat it using a scripting language?
Thanks
You should be able to get all of the information you need using a standard join, and then, with the concat function appropriate to your database (see here http://www.1keydata.com/sql/sql-concatenate.html) you can form the string you want.
Your question is very vague.
I'm guessing you are talking about matching on a primary key called partnumber or something like that.
You can do this using a query like
SELECT mp.partnumber
, mp.UPC_number
, wp.UPC_number
, pp.UPC_number
FROM metal_parts mp
INNER JOIN wood_parts wp ON (wp.partnumber = mp.partnumber)
INNER JOIN plastic_parts pp ON (pp.partnumber = mp.partnumber)
WHERE mp.partnumber = '8874578127';
You can also do
SELECT mp.partnumber
, group_concat(mp.UPC_number) as metal_UPCs
, group_concat(wp.UPC_number) as wood_UPCs
, group(concat(pp.UPC_number) as plastic_UPCs
FROM metal_parts mp
INNER JOIN wood_parts wp ON (wp.partnumber = mp.partnumber)
INNER JOIN plastic_parts pp ON (pp.partnumber = mp.partnumber)
WHERE mp.partnumber = '8874578127'
GROUP BY mp.partnumber;
or
SELECT mp.partnumber
, concat_ws(','
, group_concat(mp.UPC_number)
, group_concat(wp.UPC_number)
, group(concat(pp.UPC_number)
) as UPCs_of_parts
FROM metal_parts mp
INNER JOIN wood_parts wp ON (wp.partnumber = mp.partnumber)
INNER JOIN plastic_parts pp ON (pp.partnumber = mp.partnumber)
WHERE mp.partnumber = '8874578127'
GROUP BY mp.partnumber;