简体   繁体   English

MySQL在一行中选择多行

[英]MySQL select multiple rows within a row

I want something like this, not sure if my syntax is correct though. 我想要这样的东西,不知道我的语法是否正确。 I will be executing this with php. 我将使用php执行此操作。

SELECT 
    a.column1,
    a.column2,
    b.column1,
    c.column1,
    IF a.column3 NOT NULL
        THEN (
            SELECT c.column1, c.column2, c.column3, d.column1
            FROM table_d d
            INNER JOIN table_c c
                ON d.column1 = c.column1 AND c.column4 = 1
            WHERE d.column2 = a.column3
        );
    END IF;

FROM table_a a

    INNER JOIN table_b b
        ON a.column1 = b.column1 AND b.column2 = 1

    INNER JOIN table_c c
        ON a.column1 = c.column1 AND c.column2 = 1

WHERE
    a.column1 = 1000
    AND b.column3 = 1
    AND c.column3 = 0

ORDER BY 
        a.column1 ASC

So the output will be something like this: 所以输出将是这样的:

It would be ok if it has multiple rows with the same data on the first few columns. 如果它在前几列上有多个具有相同数据的行,那就没问题了。 Something like this: 像这样的东西:

grey area is from the outer SELECT and white area is from the inner SELECT 灰色区域来自外部SELECT ,白色区域来自内部SELECT

Note that both outer and inner select statement has table_c. 请注意,外部和内部select语句都有table_c。 If without the IF statement, can I do this? 如果没有IF声明,我可以这样做吗?

SELECT 
    a.column1,
    a.column2,
    b.column1,
    c.column1,
    cc.column1,
    cc.column2,
    cc.column3,
    d.column1   

FROM table_a a

    INNER JOIN table_b b
        ON a.column1 = b.column1 AND b.column2 = 1

    INNER JOIN table_c c
        ON a.column1 = c.column1 AND c.column2 = 1

    LEFT JOIN table_d d
        ON  a.column3 = d.column2

    INNER JOIN table_c cc
        ON d.column1 = cc.column1 AND cc.column4 = 1

WHERE
    a.column1 = 1000
    AND b.column3 = 1
    AND c.column3 = 0

ORDER BY 
        a.column1 ASC

It kinda feels wrong to me. 这对我来说有点不对劲。 What if I use fetch_assoc ? 如果我使用fetch_assoc怎么fetch_assoc Is it even possible to do this in one query? 甚至可以在一个查询中执行此操作吗?

Try something like this: 尝试这样的事情:

SELECT 
    a.column1,
    a.column2,
    b.column1,
    c.column1,
    IF (a.column3 NOT NULL,c_2.column1,''),
    IF (a.column3 NOT NULL,c_2.column2,''),
    IF (a.column3 NOT NULL,c_2.column3,''),
    IF (a.column3 NOT NULL,d.column4,'')

FROM table_a a

    INNER JOIN table_b b
        ON a.column1 = b.column1 AND b.column2 = 1

    INNER JOIN table_c c
        ON a.column1 = c.column1 AND c.column2 = 1

    INNER JOIN table_c as c_2
        ON d.column1 = c.column1 AND c.column4 = 1

    INNER JOIN table_d 
        ON a.column1 = c.column1 AND c.column2 = 1
WHERE
    a.column1 = 1000
    AND b.column3 = 1
    AND c.column3 = 0
ORDER BY 
        a.column1 ASC

To use the fetch_assoc, you would need to use aliases, otherwise you would only be able to fetch them by index, really. 要使用fetch_assoc,您需要使用别名,否则您只能通过索引来获取它们。 Other than that, it should work. 除此之外,它应该工作。

IF a.column3 NOT NULL THEN 如果a.column3 NOT NULL那么

This is what "inner join" is for. 这就是“内部联接”的用途。 By definition the inner really means "only if there's a match". 根据定义,内部真正意味着“只有匹配”。 Alternately you can use left outer join and then use a "where" to be sure that no records that have NULL in this position are included. 或者,您可以使用左外连接,然后使用“where”以确保不包含此位置中具有NULL的记录。 Inner Join will filter out any records that have null in the Match field (ie: NO match = exclude this record). 内部联接将过滤掉匹配字段中具有空值的任何记录(即:否匹配=排除此记录)。

If you want all the records, but you want blanks in these fields if the linked table has no record ... that's what left outer join is for. 如果你想要所有记录,但是如果链接表没有记录那么你想在这些字段中显示空白......那就是左外连接的用途。 Once again, that's actually the definition. 再一次,这实际上是定义。 8-) 8-)

If by fetch_assoc you refer to a PHP function, you should consider switching to mysqli instead of mysql which allows more readily available "index or name" field association. 如果你通过fetch_assoc引用PHP函数,你应该考虑切换到mysqli而不是mysql,它允许更容易获得的“索引或名称”字段关联。 But in any case, the inner join could remove the need to use "as XXXX" field name aliasing. 但无论如何,内部联接可以消除使用“as XXXX”字段名称别名的需要。

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

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