how to use union in doctrine query symfony 1.4? - doctrine

I want to have query like this in mysql:
select a.x, a.y, a.foo, sum(a.bar) as bar_sum from mytable a groupby a.x union select a.x, a.y, a.bar, sum(a.foo) as foo_sum from mytable a groupby a.y
this format is true? and how I can convert it to doctrine query in symfony?
thanks a lot

I believe that UNION is not supported using DQL (doctrine query language). But it looks like someone was able to get around the issue, using a nasty heredoc.
Or, you could just add a custom method in an object peer class and write the query in native sql.
-- Edit --
Use the following as an example (untested):
// Assuming you have the default doctrine setup, where non-object classes are
// appended with Table (your object being Foo)
class FooTable
{
public static function getFooBarUnion()
{
$sql = "select a.x, a.y, a.foo, sum(a.bar) as bar_sum from mytable a groupby a.x union select a.x, a.y, a.bar, sum(a.foo) as foo_sum from mytable a groupby a.y";
$results = Doctrine_Query::create()->query($sql);
return $results;
}
}

Related

Eloquent Subquery using models

i want to turn this raw sql to eloquent
SELECT x.* from
(SELECT * from clients where office_id IN(74,129,19)) AS x
where
x.firstname like '%parker%' or
x.lastname like '%parker%'
My model is Client
Okay i found out the answer using the
use Illuminate\Database\Eloquent\Builder;
$office_ids = [74,129,19];
Client::with('office')->whereIn('office_id',$office_ids)->where(function(Builder $dbQuery){
....
})

Where Not In using query builder

Similary with this question I need to execute the following Sql query:
SELECT COUNT(*) from table where column NOT IN (SELECT table2.id from table2 where table2.someanothercolumn >0 );
Using Eloquent's query builder, therefore I tried the following (Model Table maps into table table and Model TableTwo maps into table table2):
$enties = Table::where('id',function($q){
$q->from('table2')->select('id')->where('someanothercolumn','>',0);
})->count();
But on the cide above how I can Place the NOT IN clause?
Your answer is in the following snippet of code:
$enties = Table::whereNotIn('id',function($q){
$q->from('table2')->select('id')->where('someanothercolumn','>',0);
})->count();
In other words just use the whereNotIn.

How to convert my TSQL query to LINQ

I am new to linq. I can't convert this SQL code to linq. Please help me. Thanks.
DECLARE #myHID BIGINT;
SET #myHID = 1;
WITH tblChild AS
(
SELECT *
FROM wbs.WBS w
WHERE w.ParentId = #myHID
UNION ALL
SELECT w2.*
FROM wbs.WBS w2
JOIN tblChild ON w2.ParentId = tblChild.hID
)
SELECT
tblChild.hID ,
Unit.ID, w3.wbsName + ' * ' + tblChild.wbsName as
structure ,
tblChild.FK_WbsBaseStructure_hID ,
tblChild.parentID ,
unitNumber ,
unitTitle ,
FK_UsageItem_ID,
usageTitle ,
nominalArea
FROM
tblChild
INNER JOIN
unit.Unit ON tblChild.hID = Unit.FK_WBS_hID
INNER JOIN
unit.UsageItem ON Unit.FK_UsageItem_ID = UsageItem.ID
LEFT JOIN
wbs.WBS w3 ON tblChild.parentID = w3.hID
Please convert this to linq code.
Thanks.
For translating SQL to LINQ query comprehension:
Translate FROM subselects as separately declared variables.
Translate each clause in LINQ clause order, leaving monadic operators (DISTINCT, TOP, etc) as functions applied to the whole LINQ query.
Use table aliases as range variables. Use column aliases as anonymous type field names.
Use anonymous types (new { }) for multiple columns
Left Join is simulated by using a into join_variable and doing another from from the join variable followed by .DefaultIfEmpty().
Replace COALESCE with the conditional operator and a null test.
SELECT * must be replaced with select range_variable or for joins, an anonymous object containing all the range variables.
SELECT fields must be replaced with select new { ... } creating an anonymous object with all the desired fields or expressions.
Proper FULL OUTER JOIN must be handled with an extension method.

Unoin clause in querydsl 4.1.3?

How can I use union clause in querydsl 4.1.3?
I want just to use union clause. Need an example.
#sql
select * from (
select a,b from tableA union
select a,b from tableB
)
QueryDSL - how to join to a union of subqueries
I can't find SQLSubQuery class in querydsl lib(version 4.1.3)
I'm using
* spring boot
* spring-data
* querydsl(4.1.3)
Its perfectly possible for QueryDSL SQL queries:
QSurvey survey1 = new QSurvey("survey1");
QSurvey survey2 = new QSurvey("survey2");
QSurvey survey3 = new QSurvey("survey3");
SQLQuery<Void> query = new SQLQuery<Void>();
query.with(survey1, select(survey1.all()).from(survey1));
query.union(
select(survey2.all()).from(survey2),
select(survey3.all()).from(survey3));
The last statement returns a subquery that yields the union of the two queries.

Using Mysql WHERE IN clause in codeigniter

I have the following mysql query. Could you please tell me how to write the same query in Codeigniter's way ?
SELECT * FROM myTable
WHERE trans_id IN ( SELECT trans_id FROM myTable WHERE code='B')
AND code!='B'
You can use sub query way of codeigniter to do this for this purpose you will have to hack codeigniter. like this
Go to system/database/DB_active_rec.php
Remove public or protected keyword from these functions
public function _compile_select($select_override = FALSE)
public function _reset_select()
Now subquery writing in available
And now here is your query with active record
$this->db->select('trans_id');
$this->db->from('myTable');
$this->db->where('code','B');
$subQuery = $this->db->_compile_select();
$this->db->_reset_select();
// And now your main query
$this->db->select("*");
$this->db->where_in("$subQuery");
$this->db->where('code !=', 'B');
$this->db->get('myTable');
And the thing is done. Cheers!!!
Note : While using sub queries you must use
$this->db->from('myTable')
instead of
$this->db->get('myTable')
which runs the query.
Watch this too
How can I rewrite this SQL into CodeIgniter's Active Records?
Note : In Codeigntier 3 these functions are already public so you do not need to hack them.
$data = $this->db->get_where('columnname',array('code' => 'B'));
$this->db->where_in('columnname',$data);
$this->db->where('code !=','B');
$query = $this->db->get();
return $query->result_array();
Try this one:
$this->db->select("*");
$this->db->where_in("(SELECT trans_id FROM myTable WHERE code = 'B')");
$this->db->where('code !=', 'B');
$this->db->get('myTable');
Note: $this->db->select("*"); is optional when you are selecting all columns from table
try this:
return $this->db->query("
SELECT * FROM myTable
WHERE trans_id IN ( SELECT trans_id FROM myTable WHERE code='B')
AND code!='B'
")->result_array();
Is not active record but is codeigniter's way http://codeigniter.com/user_guide/database/examples.html
see Standard Query With Multiple Results (Array Version) section
you can use a simpler approach, while still using active record
$this->db->select("a.trans_id ,a.number, a.name, a.phone")
$this->db->from("Name_Of_Your_Table a");
$subQueryIn = "SELECT trans_id FROM Another_Table";
$this->db->where("a.trans_id in ($subQueryIn)",NULL);

Resources