This question already has answers here:
Why does using a subquery inside a left join give a completely different answer than an equivalent table?
(2 answers)
Access 2007 - Left Join to a query returns #Error instead of Null
(3 answers)
Closed 1 year ago.
I have two tables in Access:
Table1:
ID
1 A
2 B
3 C
Table2:
ID
1 A
2 B
3 D
and a query (Query1) that adds a new column to Table 1:
SELECT [Table1].ID
,"NEW CATEGORY" AS Category
FROM [Table1]
Which looks like this:
ID Category
1 A NEW CATEGORY
2 B NEW CATEGORY
3 C NEW CATEGORY
Now, if I join Query1 to Table2, like this:
SELECT [Table2].ID
,[Query1].ID
,[Query1].Category
FROM [Table2]
LEFT JOIN [Query1] ON ([Table2].ID = [Query1].ID)
I get the following result:
Table2.ID Query1.ID Category
1 A A NEW CATEGORY
2 B B NEW CATEGORY
3 D NEW CATEGORY
"NEW CATEGORY" magically appears in row #3 in the Category column, whereas I am expecting an empty cell there.
Any ideas of the source of this strange behavior, or how it can be avoided?
Related
This question already has answers here:
Can I concatenate multiple MySQL rows into one field?
(16 answers)
Closed 1 year ago.
I have one table called forms and one table called categories. They have a many-to-many relationship through a category_form as pivot. The pivot table has form_id and category_id
So each form can have many categories and every category can be attached to many forms.
I need to aggregate this data somehow. What I need is a comma separated list of category id's in a column for each form.
Like this:
FORMS
id | name | categories
1 | f1 | 1,4,7
2 | f2 | 1
3 | f3 | 1,6,8,9
What would be an effective way of doing this?
Try this:
SELECT f.id, f.name, GROUP_CONCAT(cf.category_id) AS categories
FROM forms f
INNER JOIN category_form cf ON f.id = cf.form_id
GROUP BY f.id, f.name
ORDER BY f.id;
This question already has answers here:
Return row only if value doesn't exist
(2 answers)
Closed 5 years ago.
For example table-A (id,number)
table-B(id,number)
now I want to delete Table-B records if id,number combination does not exist in table-A.
Can you any one help me on this?
sample data:
table-A
11,1001
12,1231
13,3451
table-B
11,3451
12,1231
54,1001
so i need to delete 11,3451; 54,1001 records from table-B
What about something like this to get the IDs that need to be deleted:
SELECT ID
FROM Table-B b
LEFT JOIN Table-A a on b.id = a.id AND b.number = a.number
WHERE a.id IS NULL
This question already has answers here:
How to join two columns to the same table [duplicate]
(3 answers)
Closed 5 years ago.
I want to join 2 column that share same foreign key in another table
here's the tables:
country:
idcountry| countryname
1 german
2 america
destination
id|fromcountry |tocountry
1 1 2
the result that i wanted to:
id|fromc |toc
1 german america
Use simple Left Join ans provide alias to country name column:
SELECT d.idcountry as id, cf.CountryName as fromc, ct.CountryName as toc
FROM destination d
LEFT JOIN country cf ON d.fromcountry = cf.idcountry
LEFT JOIN country ct ON d.tocountry = ct.idcountry
Use Left Outer Join for both fields
something like this
SELECT Dest.ID, CFrom.CountryName, CTo.CountryName
FROM Destination Dest
LEFT OUTER JOIN Country CFrom ON Dest.FromCountry = CFrom.idcountry
LEFT OUTER JOIN Country CTo ON Dest.ToCountry = CTo.idcountry
I am planning to create a website similar to IMDB.com. To reduce execution time I am using the following structure. Is it okay for faster working?
Table - 1
Id Movie_name description
1 name one some description
2 name two some description
3 name three some description
Table 2
id actorname
1 name 1
2 name 2
3 name 3
4 name 4
Table 3
id movieid actorid
1 1 1
2 1 2
3 1 3
4 1 9
5 2 6
6 2 5
7 2 8
8 2 1
When I want to list actors in a movie program will retrieve actors ids from table 3 and find respective names from table 2 (using single query). When I want to list the movies of a actor it will retrieve movie ids from table 3 and find respective names from first table. Will it work properly? Any other ideas?
This will give all actors in a specified movie,
SELECT c.ID, c.actorName
FROM table1 a
INNER JOIN table3 b
ON a.ID = b.movieID
INNER JOIN table2 c
ON b.actorid = c.ID
WHERE a.ID = 1
This one will give all movies for a specified actor
SELECT a.*
FROM table1 a
INNER JOIN table3 b
ON a.ID = b.movieID
INNER JOIN table2 c
ON b.actorid = c.ID
WHERE c.ID = 1
SQLFiddle Demo (both queries)
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
UPDATE 1
This is called Relational Division
SELECT a.ID, a.Movie_Name
FROM table1 a
INNER JOIN table3 b
ON a.ID = b.movieID
INNER JOIN table2 c
ON b.actorid = c.ID
WHERE c.ID IN (1, 2, 3)
GROUP BY a.ID, a.Movie_Name
HAVING COUNT(DISTINCT c.ID) = 3
SQL of Relational Division
I suggest that you modify table3 by taking away the id field. Use the movieid and actorid together as your primary key. You might want to add other fields to this table such as name of character and order of appearance as suggested in the comment by Jermaine Xu.
Im really struggling to get my head around what should be simple,
I have two tables, one contains records the other is a mapping table.
records
ID Title Description
1 record 1 desc 1
2 record 2 desc 2
3 record 3 desc 3
4 record 4 desc 4
mapping table
ID1 ID2
1 3
2 4
What I want to do is get the two titles of each row in the mapping table. So the above would output
record 1 record 3
record 2 record 4
Im missing something really obvious, trying multiple joins results in errors trying to link the same table twice.
The following returns NUll
SELECT records.title FROM mapping
LEFT JOIN records
ON mapping.ID1 = records.id
AND mapping.ID2 = records.id
try this one: (UNTESTED)
SELECT b.Title as TitleA,
c.Title as TitleB
FROM mapping a
INNER JOIN records b
on a.ID1 = b.ID
INNER JOIN records c
on a.ID2 = c.ID