if if条件如何在select查询中使用? [英] If else condition how to use in select query?
本文介绍了if if条件如何在select查询中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
`i want a query in which if and else will use but i do not know how it will use,below is my table structure ,i want result below mentioned after Produce table ,,please help thanks....`
>Item Master Table
`item_code Item_Name Weight UOM
1001 Pen 44 unit
1002 Computer 42 unit`
Customer Table
` Customer_ID Customer_Name Contact Address
1 Akhter 3434 3434
2 Hussain 333 555`
Customer Item Table
`Customer_ItemCode Customer_ItemName Item_code(FK) Weight Customer_ID(FK)
2001 Pen America 1 100 1`
SalesOrder Table
` Order_No Customer_ID (FK) Order_Ref Date
1 1 333 333
2 2 222 222`
Produce table
` Prod_No Item_Code(FK) qty Order_No (FK)
10001 1 1 1
10002 2 1 2 `
when select 10001 record from Produce table then it result like
`(10001,Pen America,1)`
when i select record 10002 then it result like
`(10002,Computer,1)`
Please suggest...thanks
我的尝试:
What I have tried:
DECLARE @CustID as varchar
select * INTO #tmp55 from SalesOrder where CustomerID IN (select CustomerID from CustomerItem)
select @CustID = CustomerID from #tmp
if @CustID != ''
select Customeritem.CIName,Probale.Prdno,ItemMasterFile.BaleSize from CustomerItem inner join ItemMasterFile on
ItemMasterFile.Codeitem=CustomerItem.CItem inner join Probale on Probale.Codeitem=ItemMasterFile.CodeItem where Probale.Prdno=1000003
else
select Probale.Prdno,ItemMasterFile.BaleSize,ItemMasterFile.BaleSize from ItemMasterFile
inner join Probale on Probale.Codeitem=ItemMasterFile.CodeItem where Probale.Prdno=1000003
推荐答案
如果您更改查询以询问您想要询问的内容,那么它们将起作用。您已将相同的查询粘贴到这两个条件中,因此它的工作方式相同。
另外,你正在创建一个名为#tmp55的临时表,但是从#tmp读取。而不是尝试随机的东西,从一开始就开始工作。如果要使用临时表中的客户ID集合,请加入该表以获取结果集合。在开始下一个步骤之前使每个步骤都有效
If you change your queries to ask what you want to ask, they will work. You've pasted the same query in to both conditions, so it will work the same.
Also, you're creating a temporary table called #tmp55 but reading from #tmp. Instead of trying random things, start from the start and work through. If you want to work with the collection of customer ids from a temp table, join to that table to get a collection of results. Make each step work before starting on the next one
这篇关于if if条件如何在select查询中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文