我的查询通过visual basic访问数据库时出现问题。可以说我有两张桌子。
+-------+-------------+-------------+------------+
| ID | Date1 | Date2 | CustomerID |
+-------+-------------+-------------+------------+
| 1 | 1-1-2013 | 1-1-2012 | 1 |
| 2 | 1-1-2013 | 1-1-2012 | 1 |
| 3 | 1-1-2013 | 1-1-2012 | 2 |
| 4 | 1-1-2013 | 1-1-2012 | 3 |
| 5 | 1-1-2013 | 1-1-2012 | 3 |
+-------+-------------+-------------+------------+
和
+----------+---------+
| ID | Name |
+----------+---------+
| 1 | John |
| 2 | Tina |
| 3 | Patrick |
+----------+---------+
我想在第一个表格中仅使用Customer - ID
中的唯一数字得到结果,如下所示。
+----------+----------+-------------+------------+
| ID | Date1 | Date2 | CustomerID |
+----------+----------+-------------+------------+
| 1 | 1-1-2013 | 1-1-2012 | 1 |
| 3 | 1-1-2013 | 1-1-2012 | 2 |
| 4 | 1-1-2013 | 1-1-2012 | 3 |
+----------+----------+-------------+------------+
我尝试过这个查询,但没有运气。
sqL = " SELECT DISTINCT Order.ID, Order.Date1, Order.Date2, Customer.Name
FROM Order (Order INNER JOIN Customer ON Order.CustomerID = Customer.ID)"
但代码并没有给我我想要的结果。能否请我帮我查询一下。
答案 0 :(得分:2)
更新这样做
SELECT q.OrderID, o.Date1, o.Date2, o.CustomerID, c.Name AS CustomerName
FROM
(
(SELECT CustomerID, MIN(ID) AS OrderID
FROM [Order]
GROUP BY CustomerID
) AS q INNER JOIN [Order] AS o
ON q.OrderID = o.ID
) INNER JOIN Customer AS c
ON o.CustomerID = c.ID
输出:
OrderID | Date1 | Date2 | CustomerID | CustomerName --------|----------|----------|------------|------------- 1 | 1/1/2013 | 1/1/2012 | 1 | John 3 | 1/1/2013 | 1/1/2013 | 2 | Tina 4 | 1/1/2013 | 1/1/2012 | 3 | Patrick
更新:根据您的评论,您的真实查询可能会显示为
SELECT q.ID, o.ID_narocila, o.datum_sprejema, o.rok_izdobave, o.status_artikla, o.status_narocila o.ID_stranke, c.naziv
FROM
(
(SELECT ID_stranke, MIN(ID) AS ID
FROM Narocilo
GROUP BY ID_stranke
) AS q INNER JOIN Narocilo AS o
ON q.ID = o.ID
) INNER JOIN Stranke AS c
ON o.ID_stranke = c.ID
答案 1 :(得分:0)
从您显示的所需结果集中,以下查询将起作用:
SELECT Min(T2.ID) AS MinOfID, Min(T1.Date1) AS MinOfDate1, Min(T1.Date2) AS MinOfDate2, T1.CustomerID
FROM T2 INNER JOIN T1 ON T2.ID = T1.CustomerID
GROUP BY T1.CustomerID;