簡體   English   中英

我需要一些幫助以在SQL Server中構建SQL選擇查詢

[英]I need some help to build a SQL select query in SQL Server

我有一個SQL Server數據庫,我需要從兩個不同的表中獲取不同的數據。在一個查詢中,表名稱為:

表#1名稱: Barcode

它包含以下列:

  • Num ->此列是外鍵
  • Barcode --->此列是主鍵

表#2名稱: MatCard

它包含以下列:

  • Num ->此列是主鍵
  • Name
  • Price4 >此Price4零售價
  • Price24 >此列用於批發價格

我正在嘗試使用以下SQL查詢:

SELECT 
    MatCard.Name AS name, MatCard.Price4 AS price, 
    Barcode.Barcode AS code 
FROM 
    MatCard, Barcode 
WHERE
    MatCard.Num = Barcode.Num 
    AND Barcode.Barcode  = :code

該查詢運行良好,但是此查詢僅使我獲得“ Price4列”中的零售價。

當我掃描小包的條形碼時,如何從Price4列中獲得批發價,當我掃描條形碼的件時,如何從Price24列中獲得零售價?

注意:數據包的“ Barcode列,並將其與“ Barcode表中的同一“條形碼”列Barcode

謝謝

假設現有查詢確實起作用,那么我認為您只是在select子句中要求一個額外的(或不同的?)列,如下所示:

SELECT
    MatCard.Name as name
  , MatCard.Price24 as wholesale_price  -- just include the column here
  , MatCard.Price4 as retail_price
  , Barcode.Barcode as code 
FROM Barcode
INNER JOIN MatCard ON MatCard.Num = Barcode.Num 
WHERE Barcode.Barcode = :code

但是在離開之前,請學習使用SQL Standard連接語法。 幫助您完成此操作的技巧是: 停止在表名之間使用逗號 如果這樣做,則必須包括顯式聯接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM