sql - 如何在内部查询条件中使用外部查询列

标签 sql sql-server database

我想在 select 语句中使用子查询,但我想将列数据传递给子查询,以便它可以过滤正在处理的行的数据。我们如何做到这一点,或者这甚至可能吗?

select 
[SRP Code], [Master Code],  [Product Desc], [Pack Desc],[Information],  [Store],

case when ([SRP Code] <> [Master Code])
then
    isnull([38],0) + (select (e.stock/er.unit) as 'Stock' from erp_stock e join  
    erpcustomerlinking er on e.productcode = er.mastercode where 

    er.mastercode = [Master Code] <-- here I want the column

    and weekno = 38     group by e.stock,er.unit)
else
    isnull([38],0)
end as [38],[36],[37],[35],[39],[40],[41],[42],[43],[44],[45],[46],[47],[48],[49],[50]

from [dbo].[F91FE39A-F618-40A8-A6F7-C721A379EE76]

where [SRP Code] = 401794

最佳答案

是的,这是可能的。

Create table test(id int, name varchar(20), age int);
create table salary(id int, salary int, testid int);

insert into test values(1,'jitu',12);
insert into test values(2,'kapil',12);                 
insert into test values(3,'ajay',12);
insert into test values(4,'vijay',12);

insert into salary values(1, 200, 1);
insert into salary values(2, 300, 2);
insert into salary values(3, 400, 3);
insert into salary values(4, 500, 4);

select *, (select salary from salary where test.id=testid) as Salary from test

fiddle :http://sqlfiddle.com/#!2/717e9/3/0

关于sql - 如何在内部查询条件中使用外部查询列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26175960/

相关文章:

sql - 当用户输入带有特殊字符的密码时如何防止 'DROP BOBBY TABLES'?

sql-server - 如何在SQL Server 2008中创建 "No Duplicates"列?

sql-server - 为什么 SQL Server 有时会选择索引扫描而不是书签查找?

java - 将数据库字符串值与 getParameter html 值进行比较?

mysql - 查找表中没有关系的行

sql - 如何在 UPDATE 语句中使用 regexp_matches()?

sql - MS SQL Server - 游标什么时候好?

mysql - 如何处理带有时间戳和特性的数据库设计?

java - 类未找到异常 'com.mysql.jdbc.Driver'

php - 将一个值插入到一个表中,其中 id 与另一个表中给定值的 id 相匹配