如何从SQL Server Compact数据库中获取不同的记录

时间:2018-08-17 14:48:42

标签: sql sql-server sql-server-ce

如何使用SQL Server Compact从多个表中获取唯一记录?

我尝试了以下SQL语句:

SELECT Tbl_Customer.Name, Tbl_Room.Room_Number
FROM Tbl_Customer 
INNER JOIN Tbl_Room ON Tbl_Customer.Customer_number = Tbl_Room.Customer_Number 
WHERE Tbl_Customer.Customer_number IN (SELECT DISTINCT Tbl_Customer.Customer_number 
                                       FROM Tbl_Customer)

1 个答案:

答案 0 :(得分:0)

我不明白为什么会有此WHERE语句。您正在尝试从Tbl_Customer获取在同一表-Tbl_Customer中具有Customer_number的记录。没有这种情况,您可以使用

SELECT c.Name, r.Room_Number
FROM Tbl_Customer c
INNER JOIN Tbl_Room r ON c.Customer_number = r.Customer_Number 
GROUP BY c.Name, r.Room_Number

希望对您有帮助。

编辑:在这种情况下,SELECT DISTINCT应该给出相同的结果,甚至生成完全相同的查询执行计划。