[英]select distinct values from all columns
我只想從每一列中選擇不同的值。 沒有明顯的組合。 這是我的查詢,但是它返回所有列的不同組合。 我希望每一列顯示自己的不同值而不依賴其他列。
$sql= "select distinct
CASE WHEN col1 not regexp '[0-9]' THEN col1 else null end as col1
,CASE WHEN col2 not regexp '[0-9]' THEN col2 else null end as col2
,CASE WHEN col3 not regexp '[0-9]' THEN col3 else null end as col3 from table_name";
它返回以下內容:
col1| col2| col3
a 1 3
b 2 4
c 2 4
d 1 3
你們可以看到col2和col3的值不是唯一的,我也只想與col2和col3分開。 謝謝!
關於您想要的最好的猜測是使用union all
:
select distinct (case when col1 not regexp '[0-9]' then col1 end) as col1, 'col1' as which
from table_name
union all
select distinct (case when col2 not regexp '[0-9]' then col2 end) as col2, 'col2' as which
from table_name
union all
select distinct (case when col3 not regexp '[0-9]' then col3 end) as col3, 'col3' as which
from table_name;
這將產生一個包含兩列的結果,一個是列名,另一個是列中的不同值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.