如何选择语句以避免重复项目

时间:2016-02-29 10:06:23

标签: mysql sql postgresql

我可以知道如何选择一个没有重复的项目吗?这就是事情。

我在学生桌上的第一栏(例如:studentid = 123)上有相同的值。

另一列在accounts表中具有相同的值(123)。

如果我只想在学生表内部联接帐户表中选择一个值,该怎么办?我使用distinct,但它仍然提供两个值,因为其他列具有不同的值。

当前查询:

SELECT DISTINCT SAS_Student.SASI_MatricNo
    ,SAS_Student.SASI_Name
    ,SAS_Student.SASI_PgId
    ,SAS_Student.SASI_ICNo
    ,SAS_Student.SASI_CurSem
    ,SAS_AccountsDetails.TransTempCode
    ,SAS_AccountsDetails.TempAmount
    ,SAS_AccountsDetails.TempPaidAmount
    ,SAS_AccountsDetails.PostStatus
    ,SAS_AccountsDetails.Ref1
    ,SAS_AccountsDetails.NoKelompok
    ,SAS_AccountsDetails.NoWarran
    ,SAS_AccountsDetails.AmaunWarran
    ,SAS_AccountsDetails.noAkaunPelajar
    ,SAS_AccountsDetails.StatusBayaran
FROM SAS_AccountsDetails
INNER JOIN SAS_sponsorinvoice ON sas_sponsorinvoice.creditref1 = SAS_AccountsDetails.ref1
    OR sas_sponsorinvoice.creditref1 = SAS_AccountsDetails.refcode
INNER JOIN sas_student ON sas_sponsorinvoice.creditref = sas_student.sasi_matricno
WHERE sas_sponsorinvoice.batchcode = 'B000000000000303'
SASI_MatricNo SASI_Name                SASI_PgId SASI_ICNo      SASI_CurSem TransTempCode        TempAmount TempPaidAmount PostStatus Ref1  NoKelompok NoWarran AmaunWarran noAkaunPelajar StatusBayaran
"160553"      "RATHIYMALER A/P MANIAM" "32"      "911229086066"           2 "TRT000000000000054"          0              0 "Ready"    "001" ""         ""                 0 ""             ""
"160553"      "RATHIYMALER A/P MANIAM" "32"      "911229086066"           2 ""                            0              0 "Posted"   "001" ""         ""                 0 ""             ""
"160553"      "RATHIYMALER A/P MANIAM" "32"      "911229086066"           2 "TRT000000000000019"          0              0 "Ready"    "001" ""         ""                 0 ""             ""

1 个答案:

答案 0 :(得分:0)

简单的解决方案是添加到查询的末尾

Order By --(condition to bring the desired record to the top of the list)--
Limit 1

这将只返回结果集的第一行。