English 中文(简体)
I(SlectT)如何在两个表格中填满两件单质的物体?
原标题:How do I (SELECT) populate two objects from two tables with a single query?

这应该是方案人员经常遇到的问题,但我从未尝试过这样做。

这就是我的解释。 因此,我需要从表动物群中 values取价值:

 @"SELECT z.predator, z.prey FROM Zoo AS z WHERE z.preyType=@carnivore"

现在,我可以把所有价值观列入清单。 我需要在一个电网中显示该电离层的详细情况。 现在有 go子和z(为了时间目的,是为了ger、ie、各自的ids),我需要把有意义的价值观集中起来,向终端用户展示(我只能展示他们的ids)。 因此,我现在可以这样做:

 foreach (Zoo z in lstZoo)
 {
       Animal predator = GetFromAnimalTable(z.Predator)
       Animal prey = GetFromAnimalTable(z.Prey)
 }

这将使方案进展缓慢。 Can 我问一看所有细节? 与此类似:

   SELECT (SELECT * FROM ANIMAL WHERE id=z.predator), 
          (SELECT * FROM ANIMAL WHERE id=z.prey) 
   FROM Zoo AS z WHERE z.preyType=@carnivore

如果我能把价值观读到一个新的大目标? 或者这是否被视为一种不良做法?

<><>UPDATE: 这样做是否是一种标准做法? 还是像我第一次说过的那样,建议个人居住?

<><>UPDATE 2 我似乎犯了一个可怕的错误,即不像我所需要的那样把问话说出来。 我认为,为了达到我的要求,我可以 t笑这里的答案,而不必用括号构造查阅。

这里,我的实际问题是:

SELECT z.predator, p.lifeSpan, z.prey 
FROM Zoo AS z 
INNER JOIN Plants AS p ON p.parent_id=z.id 
WHERE z.preyType=@carnivore

实际上,我已经有一个INNER JOIN问询表。 现在,我需要获得Z.predator(及其相应的动物表值)、p.lifeSpan、z.prey(及其相应的动物表值)的Z.predator(SlectT)值,以满足INNER JOIN和WHERE的条件。

一部假冒的法典就是这样:

SELECT (SELECT * FROM ANIMAL WHERE id=z.predator), p.lifeSpan, (SELECT * FROM ANIMAL WHERE id=z.prey) 
FROM Zoo AS z INNER JOIN Plants AS p ON p.parent_id=z.id 
WHERE z.preyType=@carnivore

It should be pretty easy to extend my requirement from the answers here, but no success till now. I tried:

SELECT a1.*, p.lifeSpan, a2.* 
FROM Zoo AS z 
INNER JOIN Plants AS p ON p.parent_id=z.id 
INNER JOIN Animal AS a1 ON (a1.id=z.predator)
INNER JOIN Animal AS a2 ON (a2.id=z.prey)
WHERE z.preyType=@carnivore

这里有许多带有方括号的备选案文。 如何妥善安排上述询问?

最佳回答

It seems this your latest query attempt:

SELECT a1.*, p.lifeSpan, a2.* 
FROM Zoo AS z 
INNER JOIN Plants AS p ON p.parent_id=z.id 
INNER JOIN Animal AS a1 ON (a1.id=z.predator)
INNER JOIN Animal AS a2 ON (a2.id=z.prey)";
WHERE z.preyType=@carnivore

声明内抛弃半殖民地。 并摒弃双重引用。

简单地简化了《平等法》,现在将《家庭平等法》条款排除在外。

然后,你应更有能力处理进入散射发动机需要多功能的母轮。

SELECT a1.*, p.lifeSpan, a2.* 
FROM
    ((Zoo AS z 
    INNER JOIN Plants AS p ON p.parent_id=z.id) 
    INNER JOIN Animal AS a1 ON a1.id=z.predator)
    INNER JOIN Animal AS a2 ON a2.id=z.prey

通知我丢弃了随函附上的那位母子。 简便的表述并不需要。 如果你对总统府有复合表述,那么你就象这样需要父母:

ON (p.parent_id=z.id AND p.foo = z.bar)

我建议抽样调查对我正确。 (如果它为你工作,则再次加入《惠予公约》条款。) 然而,我并不密切关注括号的安置,因为我利用“准入”设计师来组建联合体......并增加“支”发动机所需要的母体。

I urge you to do the same. If you re using an Access db from Dot.Net without having a copy of Access installed, you really should get a copy. Trying to use a database without that database s native development tools is an unreasonable challenge ... somewhat like trying to type while wearing mittens.

问题回答
SELECT pred.col1 AS PredCol1, ..., pred.colx AS PredColx, 
       prey.col1 AS PreyCol1, ..., prey.colx AS PreyColx
    FROM Zoo z
        INNER JOIN Animal pred
            ON z.predator = pred.id
        INNER JOIN Animal prey
            ON z.prey = prey.id
    WHERE z.preyType = @carnivore

或者,你可能希望这样一些。

SELECT  Predator  AS AnimalType, pred.*
    FROM Zoo z
        INNER JOIN Animal pred
            ON z.predator = pred.id
    WHERE z.preyType = @carnivore
UNION ALL
SELECT  Prey  AS AnimalType, prey.*
    FROM Zoo z
        INNER JOIN Animal prey
            ON z.prey = prey.id
    WHERE z.preyType = @carnivore

表格中的条目

SELECT aPrey.Name as PreyName, aPredatior.Name as PredatorName 
FROM Zoo AS z 
LEFT JOIN Animal AS aPrey On aPrey.id= z.prey
LEFT JOIN Animal AS aPredatior On aPredatior.id= z.predator
WHERE z.preyType=@carnivore




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签