在子查询中使用外部别名,在mysql中使用4个表

时间:2018-01-19 04:21:49

标签: php mysql sql database

大家好我有4个表作为mysql中的键值存储

t1 (article):          t2:
| id | date |          | id | key     | value    |
-------------          ---------------------------
| 1  | 2016 |          | 1  | title   | title1   |
| 2  | 2017 |          | 1  | user_id | 1        |
| 3  | 2018 |          | 2  | title   | title2   |
-------------          | 2  | user_id | 2        |          
                       | 3  | title   | title3   |
                       | 3  | user_id | 1        |
                       ---------------------------

t1 (user):             t2:
| id | date |          | id | key   | value    |
-------------          -------------------------
| 1  | NULL |          | 1  | name  | user1    |
| 2  | NULL |          | 2  | name  | user2    |
-------------          -------------------------          

SELECT t1.id,
    GROUP_CONCAT(IF(t2.key='title',t2.value,NULL)) AS title,
    t1.date,
    GROUP_CONCAT(IF(t2.key='user_id',t2.value,NULL)) AS user_id,

(
SELECT GROUP_CONCAT(IF(t4.key='user_name',t4.value,NULL))
FROM t4
GROUP BY t4.id
HAVING t4.id = user_id

) AS user_name

FROM t1
    INNER JOIN t2
        ON t1.id = t2.id
GROUP BY t1.id

我想在 t2 中打印出存储为 id 的用户名称,如:

| id | title    | date   | user_id | user_name |
------------------------------------------------
| 1  | title1   | 2016   | 1       | user1     |
| 2  | title2   | 2017   | 2       | user2     |
| 3  | title3   | 2018   | 1       | user1     |
------------------------------------------------

我测试了WHERE子句和HAVING子句,但没有任何对我有用。

1 个答案:

答案 0 :(得分:1)

我发现你的表引用太混乱了,所以我使用了对示例数据的解释。顺便提一下,我只需要4个表中的3个。 Demo

MySQL 5.6架构设置

TDC_NO  REVISION    TDC_NO  ROWNO   PROP_NAME   TDC_PROPERTY
-------------------------------------------------------------
7y      1           7y      1       sizMin      2
7y      1           7y      1       sizeMax     3
7y      1           7y      1       tolMin      1
7y      1           7y      1       tolMax      2
7y      1           7y      2       sizMin      4
7y      1           7y      2       sizeMax     5
7y      1           7y      2       tolMin      6
7y      1           7y      2       tolMax      7

查询1

CREATE TABLE articles
    (`id` int, `date` date)
;

INSERT INTO articles
    (`id`, `date`)
VALUES
    (1, '2016-01-01'),
    (2, '2017-01-01'),
    (3, '2018-01-01')
;


CREATE TABLE users
    (`id` int, `date` date)
;

INSERT INTO users
    (`id`, `date`)
VALUES
    (1, NULL),
    (2, NULL)
;


CREATE TABLE t2_upper
    (`id` int, `key` varchar(7), `value` varchar(6))
;

INSERT INTO t2_upper
    (`id`, `key`, `value`)
VALUES
    (1, 'title', 'title1'),
    (1, 'user_id', '1'),
    (2, 'title', 'title2'),
    (2, 'user_id', '2'),
    (3, 'title', 'title3'),
    (3, 'user_id', '1')
;


CREATE TABLE t2_lower
    (`id` int, `key` varchar(4), `value` varchar(5))
;

INSERT INTO t2_lower
    (`id`, `key`, `value`)
VALUES
    (1, 'name', 'user1'),
    (2, 'name', 'user2')
;

<强> Results

select a.id, tn.value article_title, a.date, tu.id user_id, u.value user_name
from articles a
inner join (
    select
    *
    from t2_upper
    where `key` = 'title'
    ) tn on a.id = tn.id
inner join (
    select
    *
    from t2_upper
    where `key` = 'user_id'
  ) tu on a.id = tu.id
inner join (
    select
    *
    from t2_lower
    where `key` = 'name'
  ) u on tu.value = u.id