SQL查询两个表并组合结果(带扭曲)

时间:2016-06-26 18:49:43

标签: mysql join

我有两张表:usersusers_info

users看起来像这样:

+----+----------+-------+
| id | slug     | name  |
+----+----------+-------+
|  1 | theploki | Kris  |
+----+----------+-------+

users_info看起来像这样:

+----+--------+----------+---------------+
| id | parent | info_key | info_val      |
+----+--------+----------+---------------+
|  1 | 1      | email    | kris@kris.com |
+----+--------+----------+---------------+
|  2 | 1      | age      | 28            |
+----+--------+----------+---------------+

我想SELECT user user_info发送电子邮件='kris@kris.com'的人user_info - 和 -
返回匹配users_info.parent = user.id的所有result['email'] = 'kris@kris.com'结果 - 和 -
以可读格式(如result['id'] = 1result['name'] = 'Kris'以及result['age'] = 28info_key

我确信在另一个问题中已经回答了这个问题,但我一直在寻找和阅读对我不起作用的答案。

显然,我确保usersSELECT * FROM users WHERE 1=1 AND users.slug='theploki' AND (SELECT * FROM users_info WHERE users_info.parent = users.id AND 1=1 AND users_info.info_key = 'email' AND users_info.info_val = 'kris@kris.com' GROUP BY users_info.parent) GROUP BY users.id 列名称不匹配。

我已经尝试过INNER JOIN,OUTER JOIN,只是简单的JOIN,我尝试过没有任何JOIN。

这不起作用,但它解释了我想要做的事情:

1=1

有时我不会搜索信息列,有时我不会搜索用户列,这就是我放SELECT users.*, users_info.* FROM users INNER JOIN users_info on users_info.parent = users.id where users.id = (SELECT users_info.parent FROM users_info WHERE users_info.parent = users.id AND users_info.info_val = 'kris@kris.com')

的原因

也许只是简单地做两个查询并在之后合并它们?

UPDATE:

好的,这是更新的SQL查询(由scaisEdge提供):

+----+----------+-------+----+--------+----------+---------------+
| id | slug     | name  | id | parent | info_key | info_val      |
+----+----------+-------+----+--------+----------+---------------+
|  1 | theploki | Kris  |  1 |  1     | email    | kris@kris.com |
+----+----------+-------+----+--------+----------+---------------+
|  1 | theploki | Kris  |  2 |  1     | age      | 28            |
+----+----------+-------+----+--------+----------+---------------+

这非常接近,但会返回如下结果集:

+----+----------+-------+---------------+-----+
| id | slug     | name  | email         | age |
+----+----------+-------+---------------+-----+
|  1 | theploki | Kris  | kris@kris.com | 28  |
+----+----------+-------+---------------+-----+

但我想要一个像这样的结果集:

{{1}}

2 个答案:

答案 0 :(得分:1)

同一父用户的用户应该是

SELECT users.*, users_info.* FROM users
INNER JOIN users_info on users_info.parent_id = users.id
where users.id = (SELECT users_info.parent FROM users_info
    WHERE users_info.parent = users.id
    AND users_info.info_val = 'kris@kris.com')

仅为示例,您可以使用列名和别名更改用户。* ord users_info。*

SELECT users.slug,  users.name as name, group_concat(users_info.info_val) FROM users
INNER JOIN users_info on users_info.parent_id = users.id
where users.id = (SELECT users_info.parent FROM users_info
    WHERE users_info.parent = users.id
    AND users_info.info_val = 'kris@kris.com')
 group by users.name

答案 1 :(得分:0)

SELECT users_info.info_val AS email 
FROM users JOIN users_info  ON users_info.parent_id = users.id
WHERE users_info.info_key = 'email'

这应该适用于你想要的结果。另一个提供的答案要求您在编写查询之前知道要查找的结果。尽管如此,正如我在上面的评论中所提到的,这是构建数据库的一种奇怪方式,除非您有特定原因,否则一般用户信息(如电子邮件地址)将位于users表中。

但是,如果你的目的是拥有一个最小的用户表,然后是它们的通用键值编码信息,那么这个查询就可以了。