3 回答
TA贡献1831条经验 获得超10个赞
如果要从两个表中检索数据,则需要使用 SQL JOIN
我不确定您的表格的确切组成,但类似于以下内容
Select batch_id,
product_name,
quantity,
left_qty,
purchaseDate,
manufacturing_date,
expiryDate
from batch B
INNER JOIN Products P
ON P.P_id = B.P_id
where Convert(DATE,expiryDate,103) BETWEEN @from AND @to
TA贡献2080条经验 获得超4个赞
不确定我是否正确理解了您的问题,但我相信对于您的查询,您正在寻找一些简单的东西,例如 JOIN between Productsand Batchtables:
SELECT
P.P_id,
P.P_name,
B.batch_id,
B.product_name,
B.quantity,
B.left_qty,
B.purchaseDate,
B.manufacturing_date,
B.expiryDate
FROM Batch AS B
INNER JOIN Products AS P
ON B.p_id_fk = P.P_id
WHERE CONVERT(DATE, B.expiryDate, 103) BETWEEN @from AND @to
p_id_fk您提供的名称可能不是Batch表中的实际列名,而是外键约束本身的名称,如命名约定(_fk后缀)所示。
TA贡献1841条经验 获得超3个赞
您需要在此处加入或交叉申请。
选项 1 - 内连接:
Select
b.batch_id,pd.product_name,quantity,left_qty,
purchaseDate,manufacturing_date,expiryDate from batch b
inner join product pd on pd.p_id = b.p_id where Convert(DATE,expiryDate,103)
BETWEEN @from AND @to
选项 2 交叉应用:
Select
b.batch_id,pd.product_name,quantity,left_qty,
purchaseDate,manufacturing_date,expiryDate from batch b
cross apply
(
select product_name from product p
where p.p_id = b.p_id
) pd
where Convert(DATE,expiryDate,103)
BETWEEN @from AND @to
有关交叉申请的更多信息,请看这里。
- 3 回答
- 0 关注
- 172 浏览
添加回答
举报