访问查询以从多个表中获取记录

时间:2013-10-21 12:15:31

标签: c# ms-access

我试图从5个表中获取记录,最后我已经完成了但现在我想要独特的记录,那么我应该在查询中更改什么?

SELECT
  t.*,
  sh.invoice_Number AS sale_inv_no,
  sh.invoice_date AS sale_inv_date,
  sh.from_Date AS sale_from,
  sh.to_Date AS sale_to,
  sh.number_Of_Months AS sale_nom,
  c.cName AS cust_name,
  h.hName AS ho_name,
  [hSize1] & 'X' & [hSize2] & '=' & [hSize_SQF] AS ho_size,
  h.hLocation AS ho_loc,
  c.cMobile AS cust_mob,
  f.firmName AS firm_name
FROM
  (
    (
      (
          adv_tbl_transactions AS t
       RIGHT JOIN
          adv_tbl_Sale_Hoardings AS sh
       ON t.tra_code = sh.transaction_code
      ) LEFT JOIN adv_tbl_Hoardings AS h
        ON sh.hid = h.ID
    ) LEFT JOIN tbl_Firms AS f
      ON sh.e_fid = f.ID
  ) LEFT JOIN tbl_Customers AS c
    ON sh.cid = c.ID
WHERE
  (((t.e_fid)=1));

它给我输出如下

srNo | Hoarding    | customers   |  transaction details  | advance  | balance | total
1    | Name: h1    | Cust1       |  inv no : 1           | 6000     | 1000    | 70000
2    | Name: h1    | Cust1       |  inv no : 1           | 6000     | 1000    | 70000
3    | Name: h2    | Cust2       |  inv no : 2           | 3000     | 1000    | 40000
4    | Name: h2    | Cust2       |  inv no : 2           | 3000     | 1000    | 40000

我想输出如下

srNo | Hoarding    | customers   |  transaction details  | advance  | balance | total
1    | Name: h1    | Cust1       |  inv no : 1           | 6000     | 1000    | 70000
2    | Name: h2    | Cust2       |  inv no : 2           | 3000     | 1000    | 40000

我想要distict列entry_no

1 个答案:

答案 0 :(得分:0)

使用distinct关键字:

select distinct t.*,
       sh.invoice_Number AS sale_inv_no,
       sh.invoice_date AS sale_inv_date,
       sh.from_Date AS sale_from,
       sh.to_Date AS sale_to,
       sh.number_Of_Months AS sale_nom,
       c.cName AS cust_name,
       h.hName AS ho_name,
       [hSize1] & 'X' & [hSize2] & '=' & [hSize_SQF] AS ho_size,
       h.hLocation AS ho_loc,
       c.cMobile AS cust_mob,
       f.firmName AS firm_name
FROM (((adv_tbl_transactions AS t 
        RIGHT JOIN adv_tbl_Sale_Hoardings AS sh 
        ON t.tra_code = sh.transaction_code)
        LEFT JOIN adv_tbl_Hoardings AS h
        ON sh.hid = h.ID)
        LEFT JOIN tbl_Firms AS f ON sh.e_fid = f.ID)
        LEFT JOIN tbl_Customers AS c
        ON sh.cid = c.ID
WHERE (((t.e_fid)=1)) ;