简体   繁体   English

如何比较一行中两列之间的值(如果不同则创建另一行)?

[英]How to compare value between two column in one row, if different then create another row?

I have the following table in MySQL: 我在MySQL中有下表:

id | item_id | uom | uom1
1    1         kg    box

When i select all from the table, it will display one row. 当我从表中选择全部时,它将显示一行。

I want to make it to display two row if the uom and uom1 is different. 如果uom和uom1不同,我想使其显示两行。

For example, uom = kg and uom1 = box then result will show 例如,uom = kg和uom1 = box,则结果将显示

id | item_id | uom
1    1         kg
2    1         box

if both uom and uom1 is same(kg) then only one row will show 如果uom和uom1相同(kg),则仅显示一行

id | item_id | uom
1    1         kg

Is it possible to do it using sql? 是否可以使用sql做到这一点? Then i will just loop it at my view. 然后我将其循环显示。

select 
  @rownum := @rownum + 1 AS id,
  T.* 
from (
    select  `item_id`, `uom`
    from Table1

    union

    select  `item_id`, `uom1` as `uom`
    from Table1
) T , (SELECT @rownum := 0) r

| id | item_id | uom |
|----|---------|-----|
|  1 |       1 |  kg |
|  2 |       1 | box |

DEMO SQL Fiddle 演示SQL小提琴

One simple way of doing it would be 一种简单的方法是

select id, item_id, uom from tbl union
select id, item_id, uom1 from tbl

UNION will by default filter out duplicate lines. 默认情况下, UNION会过滤掉重复的行。 There is no need for further "tricks" ... 不需要其他“技巧” ...

@Neeraj Wadhwa suggested the second line to be changed to: @Neeraj Wadhwa建议将第二行更改为:

select id, item_id, uom1 as uom from tbl

This is possible and will produce the same result, but it is not really necessary. 这是可能的,并且将产生相同的结果,但并不是必须的。 The column names in a UNION construct will be determined by the first select statement anyway. 无论如何, UNION构造中的列名称将由第一条select语句确定。

I think the simple union should help here: 我认为简单的工会应该在这里有所帮助:

select distinct * from (
  select id, item, uom1 as uom from stuff
    union
  select id, item, uom2 as uom from stuff
  ) as unionised
order by unionised.id

Hre's the fiddle: http://sqlfiddle.com/#!9/ece687/1 赫尔的小提琴: http ://sqlfiddle.com/#!9/ece687/1

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

相关问题 如何将值从一列添加到另一列和行? - How to add value from one column to another column and row? 在mysql中,如何仅选择一列中具有相同值但在另一列中具有不同值的每一行? - In mysql, how do I select ONLY every row that has the same value in one column but different ones in another? 如何在一行中获取具有相同 id 但在另一列中具有不同值的数据并在 php 中的表中显示一个月 - How to fetch data with same id in a row but different value in another column and display one month into table in php 如何在MySQL中比较表的两个不同行 - How to compare two different row of a table in Mysql 如何使用一列的值来检查另一行的值? - How use the value of one column to check the values of another row? 如何将一列的值复制到同一行的另一列? - How do I copy the value of one column into another in the same row? 按行的值分隔一列到两列 - Separate one column to two by row's value MySQL将一个表中字段的值与另一个表中的行值进行比较 - MySQL compare value of field in one table with value of row in a different table 一行(两列的平均值)和同一行(另一列的值)的总和 - sum of one row's ( two column's avg value ) and that very same row's (another column's value) 如何使用sql比较单个表中两行的两列值? - How to compare two column value of two row in a single table using sql?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM