简体   繁体   English

在MySQL中的两个表中选择查询

[英]Select query in two tables in MySQL

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 : 我的一个表看起来像这个名字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 : 另一个表看起来像这个名字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 如何让它选择查询,以便可以从两个表中获取与userid 9相关的所有值,并使其看起来像这样

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!) (提前!)

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 http://sqlfiddle.com/#!2/39861/2

SELECT * FROM user_meta um, user u WHERE um.userid = u.userid
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

simple as this: 这很简单:

select * from user_meta, user where user_meta.userid=9 
and user_meta.userid=user.userid

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

If you want only user_meta table all values means use this. 如果您只想要user_meta表,则所有值都表示使用此值。

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM