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

时间:2014-01-16 09:09:30

标签: php mysql sql

我正在运行此查询

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

但这不是我想要的结果,它返回多行。

我的一个表格看起来像这个名字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

另一个表格看起来像这个名称user

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

如何让它选择查询,以便可以从两个表中获取与userid 9相关的所有值,并使其看起来像这样

期望的输出:

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

谢谢! (提前!)

8 个答案:

答案 0 :(得分:2)

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

上述答案与您的初始要求有关。但是根据你的新要求,它不能通过简单的查询来完成,它需要是动态的,在这里我已经为你创建了演示,你可以使用这个

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

答案 1 :(得分:0)

SELECT * FROM user_meta um, user u WHERE um.userid = u.userid

答案 2 :(得分: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

答案 3 :(得分:0)

这很简单:

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

答案 4 :(得分:0)

将其缩减为单行的唯一真正方法是使用聚合函数

例如,将细节分组到字段

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

答案 5 :(得分:0)

如果您只想要user_meta表,则所有值都表示使用此值。

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

如果您只想要用户表,则所有值都表示使用此值。

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

如果你想要两种方式都像这样提及

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

答案 6 :(得分:0)

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

答案 7 :(得分:0)

为您提供所需的输出(但这不灵活,因此如果您添加要输出的更多详细信息,则无需更改即可处理): -

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