MySQL table schema: entity (key is entityId), post (key is entityId), photo (key is entityId)
Entity is going to be 1:1 to the combined post+photo tables, as these are basically subcategories of entity
sql request:
SELECT * FROM entity
LEFT JOIN post ON post.entityId = entity.entityId
LEFT JOIN photo ON photo.entityId = entity.entityId
This does return the full schema, but for entities that do not have a photo, the entityId returned is null. Every row that doesnt have a corresponding photo has its entityId set to null.
QUESTION: What is the cleanest way to combine these subgroups while maintaining all the data? I did manage to do it by doing only one, normal join with posts, and then union-ing that with another entity joined with photos, but my solution is very messy and theres probably a better way to do it.
You need to list out the columns explicitly. The problem you are having is the columns have the same name. For example:
SELECT e.*, pid as post_id, ph.id as photo_id
FROM entity e LEFT JOIN
post p
ON p.entityId = e.entityId LEFT JOIN
photo ph
ON ph.entityId = e.entityId;
If the only columns that have duplicate ids are the entityId columns, then you can use the USING clause (assuming your database supports it):
SELECT *
FROM entity e LEFT JOIN
post p
USING (entityId) LEFT JOIN
photo ph
USING (entityId)
Related
I have 4 tables in a mysql db. A joining table, Author table, Output Table, and a Person table. I had to import a csv file that had an outputID and personID but only have access to the author names column, so i've added an auto increment primary key to the author table.
I want the query I've made to insert the author Id's into the joining table where the specific author related to the ID matches the specific outputID.
The following query keeps inserting the author Id's but all at the bottom of the the joining table, without any matching output id
INSERT INTO output(a_fk)
SELECT a.Author_ID
FROM authors a
INNER JOIN wholecsv w ON a.Author_Names = w.a_Author
INNER JOIN outputlist ot ON ot.Output_ID = w.a_ID
INNER JOIN output o ON o.Output_ID = ot.Output_ID
The following query keeps inserting the author Id's but all at the bottom of the the joining table, without any matching output id
That's what INSERT does : adding new records to the table.
It looks like you are actually looking for an UPDATE query. For this, you need to adjust the JOIN logic so it goes from output to authors, like :
UPDATE output o
INNER JOIN outputlist ot ON o.Output_ID = ot.Output_ID
INNER JOIN wholecsv ON ot.Output_ID = w.a_ID
INNER JOIN authors ON a.Author_Names = w.a_Author
SET o.a_fk = a.Author_ID
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
The code is retreating what I want it to retrieve, though it does 4 times more that I need.
I have a table called property with a PK property_ID. A property has a title, short description, long description and it belongs to a point of interest, which in turn is connected to a city. Title, short&long description have all independent link tables, in which a link is created between a property and the corresponding translation in the translation table.
I am using left joins to collect all the desired information, below in the image attachment you can see the results.
The desired result would be that it would return only 2 rows, with the corresponding translation of the title, short and long description. At the moment is returning 8 rows.
The issue that I noticed is it orders the columns in a weird way.
If you look closer, you can notice that title and titleLangCode are ordered correctly after titleLangCode, while long and short descriptions are ordered by their own langCode. Grouping them wont work since they are ordered differently, so ordering them has no effect, union didnĀ“t work either because of the column numbers, even tried distinct but with no avail.
The code:
select
property.*,
title_translation.title,
title_translation.langCode as titleLangCode,
short_desc_translation.shortDescription,
long_desc_translation.longDescription,
short_desc_translation.langCode as shortLangCode,
long_desc_translation.langCode as longLangCode,
property_city_poi.city_poi_link_ID
from
property
left join
title_link
on
property.property_ID = title_link.property_ID
left join
title_translation
on
title_link.title_link_ID = title_translation.title_link_ID
left join
short_desc_link
on
property.property_ID = short_desc_link.property_ID
left join
short_desc_translation
on
short_desc_link.short_desc_link_ID = short_desc_translation.short_desc_link_ID
left join
long_desc_link
on
property.property_ID = long_desc_link.property_ID
left join
long_desc_translation
on
long_desc_link.long_desc_link_ID = long_desc_translation.long_desc_link_ID
left join
property_city_poi
on
property.property_ID = property_city_poi.property_ID
where
property.property_ID = 10
Is there a possibility of somehow combing limit with group ? I tried but have not succeed.
title_link
title_link_ID
property_ID
dateCreated
title_translation
title_translation_ID
title_link_ID
langCode
title
short_desc_link
short_desc_link_ID
property_ID
dateCreated
short_desc_translation
short_desc_translation_ID
short_desc_link_ID
langCode
shortDescription
long_desc_link
long_desc_link_ID
property_ID
dateCreated
long_desc_translation
long_desc_translation_ID
long_desc_link_ID
langCode
shortDescription
If I understand this correctly, the issue is that you have multiple translation tables which have a 1 to many relationship to the property. So after the first join you have 2 rows then 4 and then 8 with all combinations of languages.
You can limit this by joining on 2 conditions.
... property p
INNER JOIN titel_link tl on p.id = tl.propertyid
INNER JOIN short_desk_link sdl on p.id = tl.propertyid AND tl.langCode = sdl.langCode
I've simplified this a little and used aliases for table names to shorten the join conditions.
Edit: I'd say this is also a sign of bad database design. you should probably introduce a table 'language' and then a mapping table property-translation mapping. Not sure if this is under your control but a setup with tables like this would be better.
Property: all the details for the property.
Language: A listing of all the languages with Id.
FieldId: A list of named columns that you have in your database.
Translation: A combination of Property, Language and Field on which you can then have a single translation for that field.
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?
Please explain to me joins in simple language. Please do not post a web link as I need to read how a developer understand it, not an author.
Best I can point you to is A Visual Explanation of SQL Joins.
The diagrams helped me a lot.
Adding the main diagrams from the linked post here.
Inner Join
Inner join produces only the set of records that match in both Table A and Table B.
Full outer join
Full outer join produces the set of all records in Table A and Table B, with matching records from both sides where available. If there is no match, the missing side will contain null.
Left outer join
Left outer join produces a complete set of records from Table A, with the matching records (where available) in Table B. If there is no match, the right side will contain null.
Given Table Person And Information
SELECT *
FROM Person INNER JOIN
Information ON Person.ID = Information.ID
Will only return rows from both tables where both tables have the same IDs. So only if the ID exists in both Person and Information will the row be returned.
SELECT *
FROM Person LEFT JOIN
Information ON Person.ID = Information.ID
Will return all rows from Person, and only those that match from Information, where it does not match, it will return NULLs
SELECT *
FROM Person LEFT JOIN
Information ON Person.ID = Information.ID
WHERE Information.ID IS NULL
Will return all rows from Person that DOES NOT HAVE an entry in Information. This shows you the list of persons that do not have their Informaton updated yet.
I'm interpreting your question to mean joins in a very general sense, not each type of join, so if this is off the mark then I apologize:
Basically, joins enable you to get data from multiple tables in a single query by adding columns to your result set. So if you had the following tables:
Books (ID, Title, AuthorID)
Authors (ID, Name)
You could get a result set that looked like this:
Book | Author
'The Sirens of Titan' | 'Kurt Vonnegut'
'The Old Man and the Sea' | 'Earnest Hemingway'
By joining the two tables together like so:
select Books.Title as Book, Authors.Name as Author
from Books
inner join Authors on Authors.ID = Books.AuthorID
An inner join is the simplest type of join; it may be difficult to understand the point of outer joins without first having a strong grasp of inner joins and their uses.