我有这个问题:
SELECT * FROM CableId WHERE
(@CblType IS NULL OR CblType LIKE @CblType)
AND (@CblStatus IS NULL OR CblStatus LIKE @CblStatus)
AND (@FromLoc IS NULL OR FromLoc LIKE @FromLoc)
AND (@ToLoc IS NULL OR ToLoc LIKE @ToLoc);
问题是,在数据库中,FromLoc
和ToLoc
作为外键存储到另一个表(MajorEquipment),并且从下拉列表中选择输入,从MajorEquipment填充。< / p>
+-----------------------------------------+ +--------------------+
| CableNumber | FromLoc (FK) | ToLoc (FK) | | MEID (PK) | Equip# |
|-----------------------------------------| |--------------------|
| 101 | 1 | 2 | | 1 | CP1 |
+-----------------------------------------+ | 2 | CP2 |
+--------------------+
我想要表现的是:
+-----------------------------------------+
| CableNumber | FromLoc (FK) | ToLoc (FK) |
|-----------------------------------------|
| 101 | CP1 | CP2 |
+-----------------------------------------+
我需要将原始查询的结构保持不变,并在其中嵌入可能的选择查询,但我无法正确地坐下来。我怎样才能做到这一点?
编辑:Here是所使用查询的原始演示,适用于需要尝试查明查询内容的人。
答案 0 :(得分:0)
试试这个
area
是第二个表名
SELECT CableNumber,a.Equip,a1.Equip FROM CableId c
inner join area a on c.FromLoc=a.MEID
inner join area a1 on c.ToLoc=a1.MEID WHERE
(@CblType IS NULL OR CblType LIKE @CblType)
AND (@CblStatus IS NULL OR CblStatus LIKE @CblStatus)
AND (@FromLoc IS NULL OR a.Equip LIKE @FromLoc)
AND (@ToLoc IS NULL OR a1.Equip LIKE @ToLoc);