我可以在不创建新表的情况下将表上的值与sql上的相同值进行比较 sql output selisih意味着价值和nilai之间的差异意味着价值
Create table tblData(
nourut int,
nama varchar(50),
nilai int
);
Create table tblData1(
nourut int,
nama varchar(50),
nilai int
);
insert into tblData values
(1,"Yuanita Prabandini Soesilo",85),
(2,"Marco Anthonius Pareira",80),
(3,"William Jaya Dwipangga",95);
insert into tblData1 values
(1,"Yuanita Prabandini Soesilo",85),
(2,"Marco Anthonius Pareira",80),
(3,"William Jaya Dwipangga",95);
select tblData.nama as namamhs,
tblData.nilai,
tblData1.nama,
tblData1.nilai,
case
when(tblData.nilai-tblData1.nilai<0)
then (tblData.nilai-tblData1.nilai)*-1
when(tblData.nilai-tblData1.nilai>=0)
then (tblData.nilai-tblData1.nilai)
end as selisih
from tblData,tblData1
where tblData1.nama<>tblData.nama
order by tblData1.nama DESC;
我创建了两个tblData,以便比较哪种方法无法在不创建新表的情况下比较同一个表上的值?
答案 0 :(得分:0)
只需要一个表,在FROM子句中,您为每个表提供一个Alias,这样您就可以将表引入查询两次:
select t1.nama as namamhs,
t1.nilai,
t2.nama,
t2.nilai,
case
when(t1.nilai-t2.nilai<0)
then (t1.nilai-t2.nilai)*-1
when(t1.nilai-t2.nilai>=0)
then (t1.nilai-t2.nilai)
end as selisih
from tblData as t1, tblData as t2
where t1.nama<>t2.nama
order by t2.nama DESC;
答案 1 :(得分:0)
SELECT
d1.nama as namamhs,
d1.nilai,
d2.nama,
d2.nilai,
CASE
WHEN (d1.nilai - d2.nilai < 0)
THEN (d1.nilai - d2.nilai) * -1
WHEN (d1.nilai - d2.nilai >= 0)
THEN (d1.nilai - d2.nilai)
END AS selisih
FROM tblData d1
INNER JOIN tblData d2 ON d1.nama <> d2.nama
ORDER BY d2.nama DESC;
答案 2 :(得分:0)
你可以试试这个:
select
a.nama,
a.nilai,
b.nama,
b.nilai,
abs(a.nilai-b.nilai)
from tbldata as a ,tbldata as b
where a.nama<>b.nama
order by b.nama desc