简体   繁体   English

mysql 从结果,如何 select 行在 2 列中具有相同的值

[英]mysql from a result, how to select rows that have same values in 2 column

In my MySQL database, the below query在我的 MySQL 数据库中,以下查询

SELECT c.cas_number, c.description c_desc, 
        sm.description sm_desc, sm.id sm_id, sm.component_id,
        c.id 
FROM starting_material sm 
    join component c ON c.id=sm.component_id;

returns a portion of data as below返回部分数据如下

在此处输入图像描述

And from this result, I am trying to select all rows which have the same value for the two columns c_decs and sm_desc从这个结果中,我试图 select 对两列 c_decs 和 sm_desc 具有相同值的所有行

I tried this query but it returns only 2 rows, but I can physically see there are many rows matching我试过这个查询,但它只返回 2 行,但我可以实际看到有很多行匹配

select r.cas_number, r.c_desc, r.sm_desc 
from (
        SELECT c.cas_number, c.description c_desc, 
                sm.description sm_desc, sm.id sm_id, sm.component_id,
                c.id 
        FROM starting_material sm 
            join component c ON c.id=sm.component_id
     ) as r
where r.c_desc like r.sm_desc;

I also tried this but still only 2 records in the result.我也试过了,但结果中仍然只有 2 条记录。

SELECT c.cas_number, c.description, sm.description 
FROM starting_material sm 
    join component c ON c.id=sm.component_id
where trim(c.description) = trim(sm.description);

Update:-更新:-

Only matching results are只有匹配的结果

在此处输入图像描述

try something like this尝试这样的事情

SELECT c.cas_number, c.description, sm.description
FROM starting_material sm 
join component c ON c.id = sm.component_id
where c.description = sm.description

Thanks, guys I did the below steps to find out the issue谢谢,我做了以下步骤来找出问题

By using string compare (STRCMP) I found out which records are not matching.通过使用字符串比较 (STRCMP),我发现哪些记录不匹配。 Mostly all records did not match (with value 1) other than 2 records (value 0)除了 2 条记录(值 0)之外,几乎所有记录都不匹配(值为 1)

SELECT c.cas_number, c.description, sm.description, STRCMP(c.description, sm.description) FROM starting_material sm join component c ON c.id=sm.component_id;

在此处输入图像描述

It turns out one of the columns sm.description have a carriage return at the end \r事实证明,其中一列 sm.description 在末尾有一个回车符 \r

The below query helped to get all matching records.下面的查询有助于获取所有匹配的记录。

SELECT c.cas_number, c.description, sm.description FROM starting_material sm join component c ON c.id=sm.component_id
where sm.description like concat(c.description, '\r');

I fixed the carriage return with below query我用以下查询修复了回车

update starting_material set description=REPLACE(description,'\r','');

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

相关问题 PHP/MySQL在排序时,SELECT结果中的某些行对于特定列具有不同的值 - PHP/MySQL Have some rows in SELECT result have different values for a particular column when sorting 如何在MySql中返回具有相同列值的行 - How to return rows that have the same column values in MySql 选择具有相同列值的行(Mysql) - Select rows with same column values (Mysql) 如何从任何列中按最高值选择MySQL行顺序? - How to select MySQL rows order by highest values from any column? 如何选择两列中具有相同值集的行,从而将第三列中的值连接起来? - How to select rows which have same set of values in two columns and hence concatenate the values in the third column? MySQL:选择除列之外仅具有唯一值的行 - MySQL: Select rows that have only unique values except for a column mysql - 我可以从同一列中选择值并在2列结果中显示它们吗? - mysql - can I select values from the same column and show them in 2 columns result? 计算具有3个列值的行的出现次数与同一个MySQL - Counting occurrences of rows that have 3 column values the same MySQL Mysql选择一列中具有相同值而另一列中具有不同值的行 - Mysql Select rows with same values in one column and different in another MySQL select 具有不同值的所有行同一列 - MySQL select all rows with different values same column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM