I have a problem in selecting data from two different tables. see example
TABLE1
UserID | Name | Description
myID01 | myname | mydescription
myID02 | myname2 | mydescription2
myID03 | myname3 | mydescription3
TABLE2
ID | UserID | Picture | Pic_Description
1 | myID01 | mypicture.jpg | This is my picture
The output must be to display all the record from TABLE1 and select only the matching UserID from TABLE1 to TABLE2 to display the Picture.
What must be the correct MYSQL statement to display my expected output? Thank you.
You can use a join to join multiple tables.
select t1.UserID as userid, t1.Name as name, t1.Description as description, t2.picture as picture
from table1 as t1
join table2 as t2
on t1.UserID = t2.UserID
You can use this
SELECT * -- You can change to your desired column
FROM TABLE1 t1
LEFT JOIN TABLE2 t2 ON t2.UserId = t1.UserId
Related
Table 1
Table 2
I want this kind of output needed in Format
+
My Current Query this
SELECT *, (SELECT id FROM tb1 where id = 1) as data_json FROM `tb5`
i want get table tb1 whole data in one column
like that
id | col1 | col2 | col3 | col4 | data_json
1 | ab | ab | uo | ty | [{'col1':1, 'col2':'ok', 'col3':'yy'},{'col1':1, 'col2':'ok', 'col3':'yy'}...]
how to change logical here tell me
how can get here whole json data from tb5 i don't want use join and specific column mention want whole tb5
help me .....!
we will need these json functions to transform your data.
select t1.*,
json_arrayagg(json_object('col1', t2.col1, 'col2', t2.col1, 'col3', t2.col3)) as data_json
from tb5 t1
inner join tb1 t2 on t2.id = t1.id
where t2.id = 1;
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
umm I'm not sure I've made the title right but its kind of hard to express it in short words.
I have to tables
table1:
id | name
1 | alice
2 | bob
table 2:
user_id | date
2 | 2014-11-1
2 | 2014-11-2
1 | 2014-11-3
as a query, if I want to show table 2 but instead of the integer numbers of user_id, I want it to show the corresponding names of the users where this info is stored in table 1.
I think this is supposed to be easy but I don't know how to get this done.
A query along the lines of -
select t1.name, t2.date
from table_1 t1 inner join table_2 t2 on t1.id = t2.user_id
Try:
SELECT t2.user_id, t1.name
FROM table1 t1 INNER JOIN table2 t2
ON t1.id = t2.user_id
This will do it.
SELECT
`b`.`name`,
`a`.`date`
FROM
table2 a
INNER JOIN table1 b ON (a.user_id = b.id)
SELECT
B.[Name]
,A.[date]
FROM [table 2] A
LEFT OUTER JOIN [table1] B
ON A.[user_id] = B.[id]
I'm trying to select the names of products stored in as an ID list in columns a different table. There are 20 columns to lookup so my normal approach would make a huge query. Can someone suggest a simple way to do this?
Table1:
id | productName
Table2
id | p1 | p2 | p3 | p4... up to p20
p1 - p20 each contain an ID number of a product from table1
I need to select rows in table2, replacing p1-p20 with the corresponding productName from table1
Thanks!
Can someone suggest a simple way to do this?
I guess this is a simple way:
SELECT
Table2.id,
T1.productName,
T2.productName,
T3.productName,
-- etc...
FROM Table2
JOIN Table1 T1 ON Table2.p1 = T1.id
JOIN Table2 T2 ON Table2.p1 = T2.id
JOIN Table3 T3 ON Table2.p1 = T3.id
-- etc...
Yes, this is a huge query, but it is simple.
You might want to reconsider your database design. Here's a suggestion:
Table1:
id | productName
Table2
id | index | productid
1 1 p1
1 2 p2
1 3 p3
...etc
Query like this:
SELECT id, index, productName
FROM Table2
JOIN Table1
ON Table2.productid = Table1.id
I have to database tables, where entities of the first Table may or may not have associated entries in the second table:
Table 1 Table 2
+-----+-----+ +-----+-------+-------+
| ID | ... | | ID | T1_ID | NAME |
+-----+-----+ +-----+-------+-------+
| 1 | ... | | 1 | 1 | p1 |
| 2 | ... | | 2 | 1 | p2 |
| 3 | ... | | 3 | 2 | p1 |
| 4 | ... | +-----+-------+-------+
+-----+-----+
I have the following queries i need to run:
Get all entities of Table_1 with a specific entry of Table_2 - That's easy, a simple Join will do...
Get all entities of Table_1, which don't have a specific entry of Table_2 associated - not so easy, but i also managed to query this with a join.
Get all entities of Table_1, which have a specific entry (A) and don't have another specific entry (B) associated, i.e. get all entities of Table_1 that have an entity of Table_2 with name=p1 and don't have an entity of Table_2 with name=p2 associated.
Is it possible to accomplish the kind of query from (3) in a single sql-statement without a sub-query?
Get all entities of Table_1, which
have a specific entry (A) and don't
have another specific entry (B)
associated, i.e. get all entities of
Table_1 that have an entity of Table_2
with name=p1 and don't have an entity
of Table_2 with name=p2 associated.
I'm having a bit of trouble understanding your criteria, but I think that is what you want:
SELECT *
FROM Table1 t1
JOIN Table2 t2 ON t1.ID = t2.t1_id
WHERE t2.name = 'p1'
AND NOT EXISTS(SELECT 'x' FROM Table2 t2_2 WHERE t1.ID = t2_2.t1_id AND t2_2.name = 'p2')
That will give you everything from Table1 that has a matching record in Table2 with name = 'p1' and DOESN'T have a matching record in Table2 with name = 'p2'. Is that what you need?
EDIT AGAIN:
I thought of a smarter way to do this that involves a static (non-correlated) subquery. This subquery will only be executed one time, rather than being executed once for every parent row in Table1. I didn't put this code through a query analyzer, but it should be significantly faster than of the queries using EXISTS(...)
SELECT *
FROM Table1 t1
JOIN Table2 t2 ON t1.ID = t2.t1_id
WHERE t2.name = 'p1'
AND t1.id NOT IN(SELECT t1_id FROM Table2 WHERE name = 'p2')
You can use an EXISTS subquery (effectively the same as doing two joins).
SELECT * FROM Table_1 AS t1
WHERE EXISTS (SELECT * FROM Table_2 AS t2 WHERE t1.Id = t2.Id AND Name='p1')
AND NOT EXISTS (SELECT * FROM Table_2 AS t2 WHERE t1.Id = t2.Id AND Name='p2')
To get all occurrences where t2 matches t1.id but not some other field do
SELECT t1.id, t2.id FROM table2 t2
INNER JOIN table1 t1 ON (t2.t1_id = t1.id AND not(t2.fieldx <=> t1.fieldx))
Note that this will also exclude rows where both fieldx are null.
If you don't want that substitute the <=> with =.
To make the variation of solutions more complete:
SELECT t1.*
FROM Table_1 t1
INNER JOIN Table_2 it2 ON t1.ID = it2.T1_ID AND it2.NAME = 'p1'
LEFT JOIN Table_2 lt2 ON t1.ID = lt2.T1_ID AND lt2.NAME = 'p2'
WHERE lt2.ID IS NULL