English 中文(简体)
如何在交响词典中创建子查询查询
原标题:How to create queries with subqueries in Symfony Doctrine

我想在学说中做这个Sql查询:

SELECT * 
FROM (
  (SELECT *, e1.stop_date as sort_date FROM `event` e1 WHERE ...) 
  UNION 
  (SELECT *, e2.start_date as sort_date FROM `event` e2 WHERE ...)
) e3 
ORDER BY e3.sort_date 

我想从与事件表相连的交响乐模型中得出结果, 比如使用:

Doctrine_Core::getTable( Event )->createQuery( e )

有什么想法吗?

最佳回答

您可以在复杂的查询中尝试原则 s DQL Languaje:

$q = Doctrine_Query::create()
        ->select( u.id )
        ->from( User u )
        ->where( u.id NOT IN (SELECT u2.id FROM User u2 INNER JOIN u2.Groups g) );

echo $q->getSqlQuery();

http://doctrine.readthedocs.org/en/latst/en/manual/dql-doctrin-query-language.html#subqueries

或者,也可以在数据库中创建一个视图,并用作更简单的“理论”查询的源表。

CREATE VIEW view_name AS
SELECT * 
FROM (
  (SELECT *, e1.stop_date as sort_date FROM `event` e1 WHERE ...) 
  UNION 
  (SELECT *, e2.start_date as sort_date FROM `event` e2 WHERE ...)
) e3 
ORDER BY e3.sort_date 

我在我的项目中使用了两种方法 并且运作良好, 它取决于你获取数据的方式, 如果你需要更新, 如果你被允许创建观点,等等。

问题回答

暂无回答




相关问题
Union in SQL Server 2005

The following is my query Select vehicleID from trip where (StartingDate between + convert(varchar(10), @StartDate,111) + and + convert(varchar(10), @EndDate,111)+ ) or (enddate between + ...

TSQL: union results from two selects (procedures?)

I have two procedures - two huge sets of selects with several sub-select and unions. I need to union results from these procedures and I still need them to exists separately. Something like that: if ...

Programming a custom union in C# (join between many objects)

Scenario I need to build a table of data from a list of objects given a specification. In example, I have a list/array of Order objects, and a string specification detailing what the data table should ...

Avoiding union by join?

My problem is on Oracle, but is probably database independent (?). I have the following tables: aa vid cb --- -- 1 10 2 15 bb vid cb --- -- 3 25 4 24 **rep* repid vid ...

Getting a distinct value across 2 union sql server tables

I m trying to get all distinct values across 2 tables using a union. The idea is to get a count of all unique values in the columnA column without repeats so that I can get a summation of all columns ...

SQL Size comparison

I have a database where I check on the size, because clients may only use a certain amount of space, aswell as they may only have a certain number of pages , which I put in the db aswell. I have the ...

热门标签