I have 2 tables. In first table
id book_name date
--------------------------
2 php 12-10-2015
3 java 12-10-2015
---------------------------
in the second table
id book_1 book_2
-------------------
12 2 3
------------------
now what will be the SQL query for viewing like this when search in SQL
id book_1 book_2
---------------------
12 php java
---------------------
You are looking for JOIN.
You JOIN table2 with table1 twice, using ALIAS to difference between them.
SELECT T2.id, A.book_name, B.book_name
FROM Table2 T2
JOIN Table1 A
ON T2.book_1 = A.id
JOIN Table1 B
ON T2.book_2 = B.id
Related
I am trying to work out how to perform an SQL UPDATE table column from Table A into Table B. The problem I have is trying to concat multiple values from Table A Column X into Table B Column Y
TableA Structure
id | Interest
1 | Bowling
2 | Swimming
1 | Basketball
TableB Structure
id | Interest_new
1 | null
2 | null
I want Table B to have following data
TableB
id | Interest_new
1 | Bowling,Basketball
2 | Swimming
This is my attempt with SQL query but it doesn't concat , just updated table B with first match
UPDATE TableB
INNER JOIN TableA ON TableB.id= TableA.id
SET TableB.id=CONCAT(TableA.id, ',')
where TableA.id= TableB.id;
You probably intend to use GROUP_CONCAT here, as you want an aggregated CSV output:
UPDATE TableB b
INNER JOIN
(
SELECT id, GROUP_CONCAT(Interest ORDER BY id) Interests
FROM TableA
GROUP BY id
) a
ON a.id = b.id
SET
Interest_new = a.Interests;
However, I actually advocate not even doing this, as storing CSV in your SQL tables is a generally bad idea. Consider just making a view of this data instead:
CREATE VIEW newInterests AS
SELECT id, GROUP_CONCAT(Interest ORDER BY id) Interests
FROM TableA
GROUP BY id;
I am not sure how to ask this correctly, but here it is:
table1
id | email
---------------------------
1 test#test.com
---------------------------
2 random#test.com
---------------------------
3 magic#test.com
---------------------------
4 example#test.com
---------------------------
5 noreply#test.com
---------------------------
6 admin#test.com
---------------------------
7 editor#test.com
table2
id | email_two
---------------------------
7 test#test.com
---------------------------
10 random#test.com
---------------------------
33 magic#test.com
---------------------------
99 example#test.com
---------------------------
109 master#test.com
---------------------------
299 blaster#test.com
Question:
How to correctly join two tables, and get not matching results by email? For example, what i need to get from both tables is:
noreply#test.com
admin#test.com
editor#test.com
master#test.com
blaster#test.com
because other emails matching each other.
Code
SELECT email_two FROM table2 b WHERE NOT EXISTS (SELECT * FROM table1 a WHERE a.email = b.email_two
This code returns only missing ones from table2, but i cant find a correct way to return missing results from two tables in one query.
Thanks for any answers.
The most suitable operation for what you want is a FULL OUTER JOIN which is unfortunately not supported in MySQL.
You can use UNION ALL instead:
SELECT email_two AS email
FROM table2 b
WHERE NOT EXISTS (SELECT * FROM table1 a
WHERE a.email = b.email_two)
UNION ALL
SELECT email
FROM table1 a
WHERE NOT EXISTS (SELECT * FROM table2 b
WHERE a.email = b.email_two)
you can use LEFT JOIN and UNION ALL to get this, below is sample query that may help you to get these records.
select t1.email as 'email' from t1 LEFT JOIN t2 on t1.email = t2.email
where t2.email is null
UNION ALL
select t2.email as 'email' from t2 LEFT JOIN t1 on t2.email = t1.email
where t1.email is null;
I have two tables:
Table 1 contains the User ID
Table 2 contains the user ID and other data I would like
The relationship is on the ID in both tables so what I would like to do is the following:
Pull all data from table 2 where a record exists in the id field in table 2 that matches an id in table 1.
Table 1 has other copies so to speak that are specific to other accounts while table 2 contains all the ids for all the other tables which is why (I think) I need a JOIN statement but I'm open to suggestions.
Table 1:
id
123456
Table 2:
id | name | age
123456 | John | 23
651123 | Mary | 22
811561 | Sarah | 21
You can use subquery as:
SELECT *
FROM table2
WHERE ID IN (SELECT ID
FROM table1)
If you need fields from table1 as well then use an inner join like:
SELECT t1.*, t2.name, t2.age
FROM table1 t1 INNER JOIN table2 t2
ON t1.id = t2.id
Your assumption is correct, you need to join:
Select * from table1 inner join table2 on table1.userid = table2.userid
The only question here is if you want to get only id's that appear on both tables (and than use inner join) or also get such that appear only on the first table as well (left join)
You should choose inner join here because table 2 always contain records for table 1.
You can use INNER JOIN here becuase you have relation between both table, query should be this:
SELECT t1.id, t2.name, t2.age
FROM table1 t1
INNER JOIN table2 t2 ON t1.id = t2.id
I have one table of like this
user|itemID
and another table like this:
itemID | itemTrait1 | itemTrait2 etc...
I am trying to link the tables in one query which should be simple. I conduct the query like so:
SELECT * FROM Table1, Table2 WHERE Table1.userID = 1 AND Table1.itemID = Table2.itemID;
The issue is that I am getting 456 results returned but if I simply run:
SELECT * FROM Table1 WHERE userID = 1;
I get 434 results. Should both these statements get the same number of results returned?
How I imagine this call working is that for every entry in Table 1 for user 1 it connects it to the item's data in Table2? I think I am missing something here.
Query to combine both tables based in itemID
select * from table1 t1
left join table t2 on t1.itemID = t2.itemID
where t1.itemID = 1;
if you are getting the more records than the records that of table1 then your second table table2 must have multiple records for itemID. for example
Table1
user | itemId
---------------
1 | 1
Table2
itemId | itemTrait1 | itemTrait2
----------------------------------------
1 | A | B
1 | C | D
now when you will apply join here it will join itemID 1 with two items of second table.
I have two tables one with ID and NAME
table 1
ID | NAME
1 | first
2 | second
3 | third
and an XREF table with ID and PARENT ID
table2
ID | PARENT ID
1 | 0
2 | 1
3 | 2
and I would like to retrieve the NAME twice like this: NAME | PARENT NAME
If it is possible to go three levels deep but with same 2-column table like this:
result table
NAME | PARENT NAME
first | NULL or EMPTY or this line the not showing at all
second | first
third | second
... then I'd like to figure that out as well.
select t1.Name, t12.Name from
table1 t1
inner join table2 t2 on t1.ID = t2.ID
inner join table1 t12 on t2.ParentID = t12.ID
This would only return 2 rows. If you want to have the first row (for ID=1) you just need to outer join instead.
Consider putting the parentid in the first table as a self-referential relationship rather than having a separate table for it.
Ex.:
table1
ID | PARENTID | NAME
---------------------------
1 NULL first
2 1 second
3 2 third
That way you would only need to join the table on itself rather than going through a 3rd table. (This is however assuming that the rows in table1 can only have a single parent, whereas your design allows one row to have multiple parents at a time)
But for your table structure, this will work:
SELECT
a.name,
c.name AS 'PARENT NAME'
FROM
table1 a
LEFT JOIN
table2 b ON a.id = b.id
LEFT JOIN
table1 c ON b.parentid = c.id
But if you made the parentid in the same table referencing id, the SQL would be reduced to this:
SELECT
a.name,
b.name AS 'PARENT NAME'
FROM
table1 a
LEFT JOIN
table2 b ON a.parentid = b.id