0

I am running this query

select * from user_meta JOIN user ON user_meta.userid=user.userid where user_meta.userid=9

But this is not resulting what i wanted, its returning multiple lines.

My one table looks like this name user_meta:

umeta_id     userid      meta_key       meta_value
 1              9         mobile        123324
 2              9         address       some address
 3              9         city          some city
 4              9         country       some country
 5              9         occupation    some details
 6              9         website       someurl
 7              9         mobile        123324
 8              9         address       some address
 9              9         city          some city
 10             10        country       some country
 11             10        occupation    some details
 12             10        website       someurl

Another table looks like this name user:

userid          username      fullname      email              role
  9             someuser       john Doe    123324@gmail.com    admin

How can i make it select the query so that all the values related to userid 9 can be fetch from both the tables and make it look like this

Desired output:

userid          username      fullname      email              role      Mobile     address     city     country    occupation     website
  9             someuser       john Doe    123324@gmail.com    admin    123123     someaddres    Somecity    somecountry    some details    someurl

Thank you! (In Advance!)

user3201500
  • 1,538
  • 3
  • 22
  • 43

8 Answers8

2
SELECT 
    *
FROM
    user
        INNER JOIN
    user_meta ON user_meta.userid = user.userid
WHERE
    user.userid = 9

The above answer was with respect to your initial requirement. But as per your new requirement it can not be done with simple query, it needs to be dynamic and here I have created the demo for you, you can use this

http://sqlfiddle.com/#!2/39861/2

Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
  • This is returning multiple line values. I just want single line. – user3201500 Jan 16 '14 at 09:17
  • 2
    can you post your desired output information, the inner join will do join to the table and finds all the match rows of join. In user_meta you have multiple rows for userid = 9 so it returns this way. There is another way using GROUP_CONCAT() function, but this will concat the data with a separator all in one row something like mobile,address,...etc for meta_key using group by. – Abhik Chakraborty Jan 16 '14 at 09:22
  • yes but i want something similar to "desired output" as i mentioned above. thank you! for your reply. – user3201500 Jan 16 '14 at 10:08
0
SELECT * FROM user_meta um, user u WHERE um.userid = u.userid
Arion
  • 31,011
  • 10
  • 70
  • 88
GuyT
  • 4,316
  • 2
  • 16
  • 30
0
select 
    user_meta.umeta_id,
    user_meta.userid,
    user_meta.meta_key,
    user_meta.meta_value,
    user.userid,
    user.username,
    user.fullname,
    user.email,
    user.role
from
    user_meta
        LEFT JOIN
    user ON user_meta.userid = user.userid
where
    user_meta.userid = 9
Arion
  • 31,011
  • 10
  • 70
  • 88
Raju Rathore
  • 149
  • 5
0

simple as this:

select * from user_meta, user where user_meta.userid=9 
and user_meta.userid=user.userid
Arion
  • 31,011
  • 10
  • 70
  • 88
draganndi
  • 135
  • 1
  • 1
  • 10
0

The only real way to reduce it to a single line is using an aggregate function

for example, grouping the details in a field

SELECT user.userid, user.username, user.fullname, user.email, user.role, GROUP_CONCAT(CONCAT_WS('-', user_meta.umeta_id, user_meta.meta_key, user_meta.meta_value))
FROM user_meta 
INNER JOIN user ON user_meta.userid=user.userid 
WHERE user_meta.userid=9
GROUP BY user.userid, user.username, user.fullname, user.email, user.role
Kickstart
  • 21,403
  • 2
  • 21
  • 33
0

If you want only user_meta table all values means use this.

select meta.* from user_meta AS meta JOIN user AS us ON meta.userid=us.userid where meta.userid=9

If you want only user table all values means use this.

select us.* from user_meta AS meta JOIN user AS us ON meta.userid=us.userid where meta.userid=9

If you want both means mention like this

select 
    meta.umeta_id,
    meta.userid, // only one userid is enough
    meta.meta_key,
    meta.meta_value,
    us.username,
    us.fullname,
    us.email,
    us.role from user_meta JOIN
    user ON meta.userid = us.userid
Puttu
  • 98
  • 1
  • 1
  • 7
0

SELECT * FROM USERS U INNER JOIN Meta_Users M ON U.userid = M.userid
WHERE U.userid = 9

0

To give you the output you want (but this isn't flexible, so won't cope without changes if you add more details that you want to output):-

SELECT user.userid, 
        user.username, 
        user.fullname, 
        user.email, 
        user.role, 
        mobile_meta.meta_value AS `Mobile`, 
        address_metameta_value AS `Address`, 
        city_metameta_value AS `City`, 
        country_metameta_value AS `Country`, 
        occupation_metameta_value AS `Occupation`, 
        website_metameta_value AS `Website`
FROM user
LEFT OUTER JOIN user_meta AS mobile_meta ON mobile_meta.userid=user.userid AND mobile_meta.meta_key = 'mobile'
LEFT OUTER JOIN user_meta AS address_meta ON address_meta.userid=user.userid AND address_meta.meta_key = 'address'
LEFT OUTER JOIN user_meta AS city_meta ON city_meta.userid=user.userid AND city_meta.meta_key = 'city'
LEFT OUTER JOIN user_meta AS country_meta ON country_meta.userid=user.userid AND country_meta.meta_key = 'country'
LEFT OUTER JOIN user_meta AS occupation_meta ON occupation_meta.userid=user.userid AND occupation_meta.meta_key = 'occupation'
LEFT OUTER JOIN user_meta AS website_meta ON website_meta.userid=user.userid AND website_meta.meta_key = 'website'
WHERE user.userid=9
Kickstart
  • 21,403
  • 2
  • 21
  • 33
  • Yes and I am trying to create a dynamic query for this , see here http://sqlfiddle.com/#!2/13703/11 , not sure where it goes wrong, if someone can edit it this might solve the issue. – Abhik Chakraborty Jan 16 '14 at 11:25
  • That should work. However I think the problem you have is a very simple typo. You have set the test data up for user id 1 yet you select for user id 9, so no records returned. Note you could use the GROUP_CONCAT suggestion I made earlier to do this, extracting the details in your script. – Kickstart Jan 16 '14 at 11:38
  • Ah @Kickstart thanks a lot it worked now, my bad and I was going crazy. http://sqlfiddle.com/#!2/39861/2 here is the output. I used the logic suggested here http://stackoverflow.com/questions/15977322/mysql-select-specific-row-values-as-column-names – Abhik Chakraborty Jan 16 '14 at 11:43
  • It is a perfectly valid way of doing this, although it will confuse most people when they see your code! Down side is that it is effectively doing 2 queries. – Kickstart Jan 16 '14 at 11:51
  • Yeah looking for a better approach if we really do not know the number of rows in the 2nd table. And you mentioned that in your answer. – Abhik Chakraborty Jan 16 '14 at 11:53
  • Fully flexible single query with not stored proc is here - http://www.sqlfiddle.com/#!2/3975b/2 . You just use explode within your script to split the combined column up (you probably want to use safer delimiters than ## and **) – Kickstart Jan 16 '14 at 12:04
  • But there is one thing i wanna know is there any dynamic way? i mean suppose "meta_key" have 100 values respective to same userid then how i will process all these? So want to make this query in some way so it will fetch all the values from table and convert it into coloums. Is there any way? i am using mysql. SO i can not use store procedures – user3201500 Jan 16 '14 at 12:58
  • Your stored procedure method does work, but is a touch difficult to read and requires 2 queries. It will return all the values in columns (potentially hundreds of them. However with SQL you return a specified number of columns defined when you do the SELECT (hence your solution effectively did one query to get the columns and another to get the data for those columns), so you can't get a result where the number of returned columns varies on each execution. You could do the same without a stored proc, just building the SQL in php in your script. – Kickstart Jan 16 '14 at 13:08
  • Yes I agree. In a situation where num of rows in the joining table is really big then just doing inner join and looping through the data and doing operation would be much efficient. – Abhik Chakraborty Jan 16 '14 at 17:48
  • Thank you Mr.Abhik. Do you know any solution related to this? – user3201500 Jan 16 '14 at 19:02
  • Honestly no , I would use regular JOINS and then display data they way it is needed using loop. – Abhik Chakraborty Jan 17 '14 at 08:04