获取行中的值作为列?

时间:2018-02-23 14:33:22

标签: mysql sql

我有一张表格中有数据显示的表格 Table Image

我跑的时候 SELECT * from pb_results where pb_results.winning_numbers LIKE '%130736%' OR pb_results.winning_numbers LIKE '%130737%' OR pb_results.winning_numbers LIKE '%130738%'    并获得结果query result

结果可以是多行,我怎么知道哪一行与哪个行相似... 例如,在上面的示例中,我想获得一个值为130736的列...这显示我的行是针对此130736 ....我该怎么做?

2 个答案:

答案 0 :(得分:1)

SELECT pb.*,  "130736" as search_value from pb_results pb where pb_results.winning_numbers LIKE '%130736%'
UNION
SELECT pb.*,  "130737" as search_value from pb_results pb where pb_results.winning_numbers LIKE '%130737%'
UNION
SELECT pb.*,  "130738" as search_value from pb_results pb where pb_results.winning_numbers LIKE '%1307378%';

将为您提供一系列search_value的结果,其中包含您要求的搜索信息。

答案 1 :(得分:-1)

您可以在select中添加一个case语句,例如

git remote set-url origin https://username:your_new_acces_token@gitlab-url/namespace/project.git

结果

drop table if exists t;
create table t(id int, List_id varchar(2), Person_id varchar(2), Company_id varchar(2));
insert into t values
(1  , 'L1'      , 'P1'        , 'C1'),
(2  , 'L1'      , 'P2'        , 'C1'),
(3  , 'L1'      , 'p3'        , 'C3'),
(4  , 'L2'      , 'P4'        , 'C4'),
(5  , 'L2'      , 'P5'        , 'C5');

select * , 
    case when person_id like '%p1%' then 'p1'
          when person_id like '%p2%' then 'p2'
          when person_id like '%p3%' then 'p3'
    end likeid
from t
where   person_id like '%p1%' or
          person_id like '%p2%' or
          person_id like '%p3%' ;