I have 4 tables: A, B, C and a linking table. I would like to know if it is possible to retrieve records from C by setting criteria in A, where there is a record in the linking table which links A and B, and a separate record in the linking table for B and C.
Here's a simplified image of the relationships:
I have tried setting up a query showing relevant fields from A, B and C, with criteria set in A. The linking table is present in the query. Running the query only returns the linked records from B. Is there something I should be putting in the criteria in the linked field of C? (I am doing this in design view for the query - my knowledge SQL is limited.)
.
SQL from Access:
SELECT Fruits.Fruit, Colour.Colour, Pests.Pest
FROM Pests RIGHT JOIN (Fruits RIGHT JOIN (Colour RIGHT JOIN [Linking Table] ON Colour.ID = [Linking Table].Colour) ON Fruits.ID = [Linking Table].Fruit) ON Pests.ID = [Linking Table].Pest
WHERE (((Pests.Pest)="Fly"));
Input: Fly
Output: Apple
Desired Output: Apple and Red
The linking table has a record linking the ID of fruit to pest, and a separate record linking the ID of fruit to colour.
Any assistance is greatly appreciated.
You're using your linking table for 2 kinds of links. This is a bad practice (normalization dictates that all columns in a table should be related, and the column pest has nothing to do with the column colour). Using one table to link color to fruit, and one to link pests to fruit would be a better plan.
You can still use this if you want to, but you will have to join in Linking Table twice (once for the fruit - pests relationship, once for the fruits - color relationship)
Try the following query:
SELECT Fruits.Fruit, Colour.Colour, Pests.Pest
FROM Pests
INNER JOIN [Linking Table] AS LT1 ON Pests.ID = LT1.Pest
INNER JOIN Fruits ON LT1.Fruit = Fruits.ID
INNER JOIN [Linking Table] AS LT2 ON Fruits.ID = LT2.Fruit
INNER JOIN Colour ON LT2.Colour = Colour.ID
This query goes from Pest to Linking table to Fruits then to a second instance of Linking table and then to Colour
Related
I have 2 tables. One (domains) has domain ids, and domain names (dom_id, dom_url).
the other contains actual data, 2 of which columns require a TO and FROM domain names. So I have 2 columns rev_dom_from and rev_dom_for, both of which store the domain name id, from the domains table.
Simple.
Now I need to actually display both domain names on the webpage. I know how to display one or the other, via the LEFT JOIN domains ON reviews.rev_dom_for = domains.dom_url query, and then you echo out the dom_url, which would echo out the domain name in the rev_dom_for column.
But how would I make it echo out the 2nd domain name, in the dom_rev_from column?
you'd use another join, something along these lines:
SELECT toD.dom_url AS ToURL,
fromD.dom_url AS FromUrl,
rvw.*
FROM reviews AS rvw
LEFT JOIN domain AS toD
ON toD.Dom_ID = rvw.rev_dom_for
LEFT JOIN domain AS fromD
ON fromD.Dom_ID = rvw.rev_dom_from
EDIT:
All you're doing is joining in the table multiple times. Look at the query in the post: it selects the values from the Reviews tables (aliased as rvw), that table provides you 2 references to the Domain table (a FOR and a FROM).
At this point it's a simple matter to left join the Domain table to the Reviews table. Once (aliased as toD) for the FOR, and a second time (aliased as fromD) for the FROM.
Then in the SELECT list, you will select the DOM_URL fields from both LEFT JOINS of the DOMAIN table, referencing them by the table alias for each joined in reference to the Domains table, and alias them as the ToURL and FromUrl.
For more info about aliasing in SQL, read here.
Given the following tables..
Domain Table
dom_id | dom_url
Review Table
rev_id | rev_dom_from | rev_dom_for
Try this sql... (It's pretty much the same thing that Stephen Wrighton wrote above)
The trick is that you are basically selecting from the domain table twice in the same query and joining the results.
Select d1.dom_url, d2.dom_id from
review r, domain d1, domain d2
where d1.dom_id = r.rev_dom_from
and d2.dom_id = r.rev_dom_for
If you are still stuck, please be more specific with exactly it is that you don't understand.
Read this and try, this will help you:
Table1
column11,column12,column13,column14
Table2
column21,column22,column23,column24
SELECT table1.column11,table1.column12,table2asnew1.column21,table2asnew2.column21
FROM table1 INNER JOIN table2 AS table2asnew1 ON table1.column11=table2asnew1.column21 INNER TABLE table2 as table2asnew2 ON table1.column12=table2asnew2.column22
table2asnew1 is an instance of table 2 which is matched by table1.column11=table2asnew1.column21
and
table2asnew2 is another instance of table 2 which is matched by table1.column12=table2asnew2.column22
I have two tables A and B. A has unique records while B may have several references to one record in A.
A table -> A.UserID,A.Image1.ID,A.Image2.ID,A.UserName
B table -> B.ImageID,B.ImageURL,ImageDescription
B. Image ID is unique and could have at least two records correspond to Image!ID and Image2ID in table A.
In my query, need to read A.UserName,B.Image1URL and B.Image2URL.
Following SQL query is to read one image. How I could modify this to read both Image1 and Image2 in one SQL query ?.
#"SELECT A.*,B.* FROM A
INNER JOIN B ON B.Image1ID = A.Image1ID
WHERE A.UserID = #Parameter1;";
So in the result, I need following :
UserID
Image1URL
Image2URL
What's the best way to get this done in mySQL ?
You can join same table twice -
SELECT A.UserID, A.UserName, img1.ImageURL, img2.ImageURL
FROM A
INNER JOIN B as img1
ON img1.ImageID = A.Image1ID
INNER JOIN B as img2
ON img2.ImageID = A.Image2ID
WHERE A.UserID = #Parameter1;
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?
I am trying to join two tables which are only related on another table (3rd table) and the identity of the two tables are foreign key of the 3rd table. Please refer to the image below so that I can fully describe what I am trying to achieve.
Relationship specs:
Actual.Id = Actual x Budget.Id
Budget.Id = Actual x Budget.Id
Budget.DateField = Actuals x Budget.DateField
And the last relationship: They are assigned as same data on Actual x
Budget.ColA
Is this achievable or should I change my database schema?
For your ActualXBudget table, don't use a single field for ID's from BOTH actual and budget. Use two columns, one for budgetID and one for ActualID. That way, a single row from ActualXBudget will be linked to both budget and Actual.
With your design, the DB has no idea that Budget ID 4 = Actual ID 1.
With Actual ID and Budget ID in the ActualXBudget Table, you can do simple joins to both tables and pull out attached records, like
SELECT Actual.ColA,Actual.ColB,Budget.ColB,Budget.ColC FROM
(Actual INNER JOIN ActualXBudget ON Actual.ID = ActualXBudget.ActualID)
INNER JOIN Budget ON Budget.ID = ActualXBudget.BudgetID
Edit: With your setup you could do
SELECT Actual.ColA,Actual.ColB,Budget.ColB,Budget.ColC FROM
(Actual INNER JOIN ActualXBudget ON Actual.ID = ActualXBudget.ID )
INNER JOIN Budget ON Budget.DateField = ActualXBudget.DateField
but don't do that.
I have 2 tables. One (domains) has domain ids, and domain names (dom_id, dom_url).
the other contains actual data, 2 of which columns require a TO and FROM domain names. So I have 2 columns rev_dom_from and rev_dom_for, both of which store the domain name id, from the domains table.
Simple.
Now I need to actually display both domain names on the webpage. I know how to display one or the other, via the LEFT JOIN domains ON reviews.rev_dom_for = domains.dom_url query, and then you echo out the dom_url, which would echo out the domain name in the rev_dom_for column.
But how would I make it echo out the 2nd domain name, in the dom_rev_from column?
you'd use another join, something along these lines:
SELECT toD.dom_url AS ToURL,
fromD.dom_url AS FromUrl,
rvw.*
FROM reviews AS rvw
LEFT JOIN domain AS toD
ON toD.Dom_ID = rvw.rev_dom_for
LEFT JOIN domain AS fromD
ON fromD.Dom_ID = rvw.rev_dom_from
EDIT:
All you're doing is joining in the table multiple times. Look at the query in the post: it selects the values from the Reviews tables (aliased as rvw), that table provides you 2 references to the Domain table (a FOR and a FROM).
At this point it's a simple matter to left join the Domain table to the Reviews table. Once (aliased as toD) for the FOR, and a second time (aliased as fromD) for the FROM.
Then in the SELECT list, you will select the DOM_URL fields from both LEFT JOINS of the DOMAIN table, referencing them by the table alias for each joined in reference to the Domains table, and alias them as the ToURL and FromUrl.
For more info about aliasing in SQL, read here.
Given the following tables..
Domain Table
dom_id | dom_url
Review Table
rev_id | rev_dom_from | rev_dom_for
Try this sql... (It's pretty much the same thing that Stephen Wrighton wrote above)
The trick is that you are basically selecting from the domain table twice in the same query and joining the results.
Select d1.dom_url, d2.dom_id from
review r, domain d1, domain d2
where d1.dom_id = r.rev_dom_from
and d2.dom_id = r.rev_dom_for
If you are still stuck, please be more specific with exactly it is that you don't understand.
Read this and try, this will help you:
Table1
column11,column12,column13,column14
Table2
column21,column22,column23,column24
SELECT table1.column11,table1.column12,table2asnew1.column21,table2asnew2.column21
FROM table1 INNER JOIN table2 AS table2asnew1 ON table1.column11=table2asnew1.column21 INNER TABLE table2 as table2asnew2 ON table1.column12=table2asnew2.column22
table2asnew1 is an instance of table 2 which is matched by table1.column11=table2asnew1.column21
and
table2asnew2 is another instance of table 2 which is matched by table1.column12=table2asnew2.column22