I have 2 tables. First table stores id in multiple column whose value is stored in other table. I want a query that returns result which has structure of my 1st table but values from 2nd table. To be more specific let's say I have a table like this:
Table A:
Uniqueid song_1 song_2 song_3 song_4 song_5
1 2 4 5 6 8
Table B:
song_id song_name
1 abcd
2 def
3 efg
4 ghi
5 abdal
6 nsadln
7 knwldn
8 jdkabdb
I want to fetch data from Table A but it should look like:
Desired result:
Uniqueid song_1 song_2 song_3 song_4 song_5
1 def ghi abdal nsadln jdkabdb
I have used join and making objects but no luck so far. Please help me out.
Just use a bunch of left joins to get to your answer:
SELECT
a.UniqueId
,s1.song_name as song_1
,s2.song_name as song_2
,s3.song_name as song_3
,s4.song_name as song_4
,s5.song_name as song_5
FROM
TableA a
LEFT JOIN TableB s1
ON a.song_1 = s1.song_id
LEFT JOIN TableB s2
ON a.song_2 = s2.song_id
LEFT JOIN TableB s3
ON a.song_3 = s3.song_id
LEFT JOIN TableB s4
ON a.song_4 = s4.song_id
LEFT JOIN TableB s5
ON a.song_5 = s5.song_id
Related
I have 3 tables :
list_routes
pk_route_id route_code route_name route_description
3 J1 IND IND-DPS
4 J4 ADT ADT_DPS
tbl_stu_route
pk_stu_route_id fk_stu_cls_id fk_route_id
2 13 3
tbl_stu_class
pk_stu_cls_id fk_stu_id
13 56
Now what I want to achieve is write query in MYSQL that it will fetch me the records from list_routes which is not associated or attached in tbl_stu_class so for e.g in the above scenario it should give me the output :
pk_route_id route_code route_name route_description
4 J4 ADT ADT_DPS
I wrote the below query using left outer join as
SELECT
a.pk_route_id,
a.route_code,
a.route_name,
a.route_description
FROM
list_routes a
left outer join tbl_stu_route b on a.pk_route_id=b.fk_route_id
left outer join tbl_stu_class c on b.fk_stu_cls_id=c.pk_stu_cls_id
where c.fk_stu_id ='56'
but the output was
pk_route_id route_code route_name route_description
3 J1 IND IND-DPS
I am not sure where I am going wrong. Can you please enlighten me how to acieve this ?
If you want to get the not exists record then just filter out with IS NULL
SELECT a.*
FROM list_routes a
left outer join tbl_stu_route b on a.pk_route_id=b.fk_route_id
left outer join tbl_stu_class c on b.fk_stu_cls_id=c.pk_stu_cls_id
where b.fk_route_id is null;
Given the nature of your question, I would use not exists:
SELECT lr.*
FROM list_routes lr
WHERE NOT EXISTS (SELECT 1
FROM tbl_stu_route sr JOIN
tbl_stu_class c
ON sr.fk_stu_cls_id = c.pk_stu_cls_id
WHERE lr.pk_route_id = sr.fk_route_id AND
c.fk_stu_id = 56
);
Notes:
When using table aliases, use abbreviations for the table names. Abbreviations make the query much easier to follow than random letters.
Do not use single quotes for numeric constants. I assume that your ids are numbers, hence 56 rather than '56'.
I find that NOT EXISTS is a better fit to your problem description than LEFT JOIN, although both work.
I have two table that each of them have a column with point (spatial) datatype like this :
Table 1 :
id latlng
------------------------------
1 POINT(35.72036 51.42124)
2 POINT(32.74446 53.42124)
3 POINT(31.78676 51.44564)
4 POINT(32.73436 54.42124)
Table 2:
id latlng
------------------------------
1 POINT(35.719 51.4221)
2 POINT(35.72036 51.42124)
3 POINT(32.74446 53.42124)
4 POINT(31.78676 51.44564)
5 POINT(32.73436 54.42124)
6 POINT(35.72379 51.4144)
Now i want to join these two table in together with LEFT JOIN statement on Table 2
i wrote this query but it returns 24 rows! i should be 6 rows like table 2.
SELECT * FROM Table2 LEFT JOIN Table1 ON Table2.latlng = Table1.latlng
is there any way to resolve this problem & just get 6 rows?
i think you forgot GROUP BY
try this
SELECT * FROM Table2
LEFT JOIN Table1
ON Table2.latlng = Table1.latlng
GROUP BY Table2.id
So I basicly have a database that looks like this:
card_types
id description price
1 Card1 1.00
2 Card2 2.00
3 Card3 3.00
card_occasions
id occasion
1 birthday
2 graduation
3 thank you
4 other
type_occasions
ID TypeID OccasionID
1 1 1
2 1 2
3 1 4
4 2 3
I am trying to do an inner join using WHERE card_type.ID = 1 resulting in a similar output but I have no idea where to begin.
Example output:
card_occasion.ID card_occasions.Name
1 birthday
2 graduation
4 other
Any help would be appreciated.
Thanks.
Since type_occasions already owns the typeid, you don't need to join the type table.
SELECT o.id, o.occassion
FROM card_occasions o
INNER JOIN type_occasions t ON t.occassionid = o.id
WHERE t.typeid = 1
You begin with the table where you want values. After Join the table you need to make the relation.
SELECT card_occasions.id, card_occasions.occasion
FROM card_occasion co
INNER JOIN type_occasions to ON (to.OccasionID = co.id)
^ the relation between two table
WHERE ct.ID = 1
SELECT A.id,A.occasion FROM card_occasions A JOIN type_occasions B ON B.OccasionID= A.id AND B.TypeID=1
And if you really want to link all three tables for reason we don't see here you may use this method. This starts linking from the type_occasion table to appropriate "basic data" tables.
Select
typeo.OccasionID, co.occasion as Name
From type_occasion typeo
JOIN card_type ct ON (typeo.TypeID=ct.id)
JOIN card_occasion co ON (typeo.OccasionID=co.id)
Where
typeo.TypeID=1
-- ct.id=1
(The example that follows is hypothetical, but illustrates the concept).
Using MySQL, say I have 2 tables:
userFromID userToId moreInfo
1 2 cat
1 3 dog
4 1 bear
3 4 fish
And...
userId someInfo addlInfo
1 m 32
2 f 33
3 m 25
4 f 28
And I want to query for a user id, and get back joined info from both tables for all users that share a relationship with user1.
assume that the first table has something like alter table thatFirstTable add unique index(userFromId, userToId) so there won't be any duplicates - each relationship between the two ids will be unique.
it doesn't matter who's the "from" or "to"
so the desired result would be something like this, if queried for relationships with user id: 1
userId moreInfo someInfo addlInfo
2 cat f 33
3 dog m 25
4 bear f 28
Thanks.
/EDIT this "works" but I suspect there's a better way?
SELECT * FROM users JOIN friends ON friends.userFrom = users.id OR friends.userTo = users.id WHERE users.id != 1 AND friends.userFrom = 1 OR friends.userTo = 1
/EDIT2 - I updated the sample output to better reflect the goal
try this query::
select tbl2.userid,tbl1.moreinfo,
tbl2.someinfo,tbl2.addinfo
from tbl1 join tbl2
on (tbl1.usertoid = tbl2.userid and tbl1.userfromid = 1)
You should just join the tables with the query below.
select u.userId, f.moreInfo, u.someInfo, u.addlInfo
from users AS u INNER JOIN friends AS f ON u.userId = f.UserToId
where f.userFrom = 1
Try this. Tested and 100% working
select a.userToID, a.moreInfo, b.someInfo, b.addInfo from tbl1 a
left outer join
tbl2 b on a.userToID = b.userId
where a.userFromID = 1;
I have three tables in mySQL:
TABLE:CollectionAttributeValues
cID
akID
avID
TABLE: atDefault
avID
value
TABLE: CollectionVersions
cID
cvName
Looks Like
CollectionVersions
cID cvName
1 Billete
5 Tony
atDefault
avID value
1 B.B
3 T.T
CollectionAttributeValues
cID akID avID
1 29 1
5 29 3
I need to take all the values (the column named values) in atDefault"
and put it into cvName in CollectionVersions
WHERE akID = 29 in CollectionAttributeValues
Basically, take "Billette" and change it to "B.B". AND also take "Tony" and change it to "TT".
So far I came up with this
SELECT value
FROM `atDefault` AS d
LEFT JOIN `CollectionAttributeValues` AS v ON d.avID = v.avID
WHERE v.akID =29
But I don't know how to insert the resulting values into the "cvName" column in CollectionVersions...
To UPDATE all the columns of the table CollectionVersions with the data that you get form the query. Try the below query -
UPDATE CollectionVersions cv
SET cvName =
(SELECT value
FROM `atDefault` AS d
LEFT JOIN `CollectionAttributeValues` AS v ON d.avID = v.avID
WHERE v.akID =29
AND cv.cID = v.cID)