简体   繁体   English

Mysql:如何检索具有相同 id 但列中值不同的两行

[英]Mysql: How can I retrieve two rows with same id but different values in column

How can I retrieve two rows with same id and same value in column and other variable.如何在列和其他变量中检索具有相同 id 和相同值的两行。 Here is the table 'data'这是表“数据”

+---------+----------------+-------------+
| post_id | meta_key       | meta_value  |
+---------+----------------+-------------+
| 1000    | payment_method | visa        |
| 1000    | other          | sometext    |
| 1000    | order_total    | 65.00       |
| 1000    | etc            | sometext2   |
| 1001    | payment_method | bacs        |
| 1001    | other          | sometext    |
| 1001    | order_total    | 105.00      |
| 1001    | etc            | sometext2   |
| 1002    | payment_method | visa        |
| 1002    | other          | sometext    |
| 1002    | order_total    | 28.00       |
| 1002    | etc            | sometext2   |
| ...     | ...            | ...         |
+---------+----------------+-------------+

As you can see payment_method has stable value and order_total is variable.如您所见, payment_method 具有稳定的值,而 order_total 是可变的。

I tried:我试过了:

SELECT * FROM 'data' WHERE meta_key IN ('payment_method', 'order_total') GROUP BY post_id, meta_key SELECT * FROM 'data' WHERE meta_key IN ('payment_method', 'order_total') GROUP BY post_id, meta_key

output output

+---------+-----------------+------------+
| post_id | meta_key        | meta_value |
+---------+-----------------+------------+
| 1000    | payment_method  | visa       |
| 1000    | order_total     | 65.00      |
| 1001    | payment_method  | bacs       |
| 1001    | order_total     | 105.00     |
| 1002    | payment_method  | visa       |
| 1002    | order_total     | 28.00      |
| ...     | ...             | ...        |
+---------+-----------------+------------+

I only want payment_method = visa and his respective amount by post_id.我只想要 payment_method = visa 和他各自的 post_id 金额。

+---------+-----------------+------------+
| post_id | meta_key        | meta_value |
+---------+-----------------+------------+
| 1000    | payment_method  | visa       |
| 1000    | order_total     | 65.00      |
| 1002    | payment_method  | visa       |
| 1002    | order_total     | 28.00      |
| ...     | ...             | ...        |
+---------+-----------------+------------+

Thank you.谢谢你。

The "problem" you have is that the data that is related is on different rows, which essentially makes it unrelated.您遇到的“问题”是相关的数据位于不同的行上,这实际上使其不相关。 You'll need to join this table to itself, or carry out a pivot operation to make the data on the same row您需要将此表连接到自身,或执行 pivot 操作以使数据位于同一行

Assuming you want the output exactly as you show:假设您想要 output 与您显示的完全一样:

SELECT * FROM data WHERE meta_key = 'payment_method' and meta_value = 'visa'

UNION ALL

SELECT a.* 
FROM data a
INNER JOIN data v 
ON
  v.meta_key = 'payment_method' and 
  v.meta_value = 'visa' and 
  a.meta_key = 'order_total' and 
  a.id = v.id

The magic happens in the second query after the union - we are again selecting all the visa rows (aliased as v) just like the query before the union but this time we are using it as a filter to restrict the order_total rows (aliased as a).奇迹发生在联合之后的第二个查询中 - 我们再次选择所有签证行(别名为 v),就像联合之前的查询一样,但这次我们使用它作为过滤器来限制 order_total 行(别名为)。 The v table contains only ids for visa, so when joined back on ID it filters the a table to only the same set of visa IDs. v 表仅包含签证的 ID,因此当重新加入 ID 时,它会将a表过滤为仅相同的签证 ID 集。 The rows in a are only order total rows, and we exclude all the v row information by only selecting a.* a中的行只是订单总行,我们只选择a就排除了所有的v行信息。*


This query is an alternative form that might be easier to understand:此查询是另一种形式,可能更容易理解:

SELECT * 
FROM data 
WHERE 
  meta_key in ('payment_method', 'order_total') and 
  ID in (SELECT x.id FROM data x WHERE x.meta_key = 'payment_method' and x.meta_value = 'visa')

It's effectively the same thing;这实际上是一回事。 create a list of ID for visa and then use it to filter the results to "only those ids" and also "only the payment method and order total rows"为签证创建一个 ID 列表,然后使用它将结果过滤为“仅那些 ID”以及“仅付款方式和订单总行”

If you ultimately want the data on the same row it might be better to pivot it right now with something like:如果您最终想要同一行上的数据,那么现在使用 pivot 可能会更好,例如:

SELECT id, 'visa' as payment_method, max(case when meta_key = 'order_total' then meta_value end) as order_total
FROM data 
WHERE meta_key IN ('payment_method', 'order_total')
GROUP BY id 
HAVING max(case when meta_key = 'payment_method' then meta_value end) = 'visa'

You can use pivoting logic to find the values you want for each key.您可以使用透视逻辑为每个键找到所需的值。 The subquery aliased as d2 aggregates by post and will return only posts whose payment method is Visa.别名为d2的子查询按 post 聚合,并将仅返回付款方式为 Visa 的帖子。 We then join your original data table to this subquery to restrict to only posts you want to see.然后,我们将您的原始data表加入到此子查询中,以仅限于您想查看的帖子。

SELECT
    d1.post_id,
    d1.meta_key,
    d1.meta_value
FROM data d1
INNER JOIN
(
    SELECT post_id
    FROM data
    GROUP BY post_id
    HAVING MAX(CASE WHEN meta_key = 'payment_method' THEN meta_value END) = 'visa'
) d2
    ON d1.post_id = d2.post_id
WHERE
    d1.meta_key IN ('payment_method', 'order_total')
ORDER BY
    d1.post_id,
    d1.meta_key DESC;

演示的屏幕截图

Demo 演示

you can use subquery and group by您可以使用subquerygroup by

select post_id
    , meta_key
    , meta_value    
from data 
where post_id in 
    (select post_id 
    from data
    where meta_key in ('payment_method', 'order_total') and meta_value='visa'
    group by post_id
    having count(1) = 2)
order by post_id, meta_value desc

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

相关问题 如何根据MySQL中的两个不同条件从同一列中检索两个值? - How do I retrieve two values from the same column based on two different conditions in MySQL? mysql,在另一列中选择两个具有相同id但值不同的不同行 - mysql, select two different rows with same id but different values in another column 如何使用两个不同的别名检索相同的列? - how can i retrieve same column with two different alias? 将具有相同id但不同列值的两行合并为一行[mysql] - merge two rows with same id but different column values into one row [mysql] MySQL-在另一列上获取具有相同ID但特定值不同的行 - MySQL - get rows with same ID but different specific values on another column SQL 合并两行ID相同但列值不同的行 - SQL Merge two rows with same ID but different column values 从数据库(MySQL)获取同一列的两行不同值的行 - Fetch two rows of different values of the same column from database (MySQL) 如何找到 mysql 中同一列不同行中的两个值之间的差异 - How to find the difference between two values in same column different rows in mysql 如何在 mysql 中的某些列中组合具有相同 ID 但不同值的行 - How to combine rows with same ID but different values in certain columns in mysql 如何检查同一ID在不同行中是否具有不同的值? - How can I check if the same ID has different values in different rows?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM