UNION and NOT IN Mysql Operation with Zend framework - zend-framework

I need to execute the following mysql query in Zend;I am not an excpert with Zend framework
SELECT `optionride`.`featureoption_id`,
`optionride`.`featureoptionride_id`,`foption`.`featureoptionblock_id`,
`foption`.`labelname`,`optionride`.`value` FROM
`engine4_ride_featureoptionrides` AS `optionride`
LEFT JOIN `engine4_ride_featureoptions` AS `foption`
ON foption.featureoption_id = optionride.featureoption_id
WHERE (optionride.ride_id = '1' ) AND (foption.featureoptiontab_id= '2' )
UNION
SELECT `foption`.`featureoption_id`, null as
`featureoptionride_id`,`foption`.`featureoptionblock_id`,
`foption`.`labelname`,null as `value` FROM `engine4_ride_featureoptions` AS `foption`
WHERE (foption.featureoptiontab_id= '2' ) AND `foption`.`featureoption_id` NOT IN
(
SELECT `optionride`.`featureoptionride_id` FROM `engine4_ride_featureoptionrides`
AS `optionride`
LEFT JOIN `engine4_ride_featureoptions` AS `foption` ON
foption.featureoption_id = optionride.featureoption_id
WHERE (optionride.ride_id = '1' ) AND (foption.featureoptiontab_id= '2' )
)
Anybody can help me please.

You can put all your query asis in the $db->fetch().
Also you can use $db->select()->union(array($sql1, $sql2)), where $sql1, $sql2 can be $db->select() or a string ("select...").
For the NOT IN you can use $db->where('someField NOT IN (?)', array());

Related

querydsl exists duplicate condition

here is my full sql as
new BooleanBuilder(
qClaimDataEntity.companyId.eq(companyId)
.or(
qClaimDataEntity.projectDataEntity.remoteProjectDataEntityList
.any().assignedCompanyId.eq(companyId)
)
.or(
qClaimDataEntity.projectDataEntity.remoteProjectDataEntityList
.any().remoteProjectStatus.eq(RemoteProjectStatusEnum.合作中)
)
);
it print sql string as
select
claimdatae0_.*
from
claim_data claimdatae0_
where
claimdatae0_.company_id=?
or exists (
select
1
from
project_data projectdat1_ cross
join
remote_project_data remoteproj2_
where
claimdatae0_.project_data_id=projectdat1_.project_data_id
and projectdat1_.project_data_id=remoteproj2_.project_data_id
and remoteproj2_.assigned_company_id=?
)
or exists (
select
1
from
project_data projectdat3_ cross
join
remote_project_data remoteproj4_
where
claimdatae0_.project_data_id=projectdat3_.project_data_id
and projectdat3_.project_data_id=remoteproj4_.project_data_id
and remoteproj4_.remote_project_status=?
) limit ?
but I think it can be done as
select
claimdatae0_.*
from
claim_data claimdatae0_
where
claimdatae0_.company_id=?
or exists (
select
1
from
project_data projectdat1_ cross
join
remote_project_data remoteproj2_
where
claimdatae0_.project_data_id=projectdat1_.project_data_id
and projectdat1_.project_data_id=remoteproj2_.project_data_id
and remoteproj2_.assigned_company_id=?
and remoteproj2_.remote_project_status=?
) limit ?
SubQuery is not support in latest Spring data jpa
and querydsl .any() don't support duplicate condition
is there another way to get the second query using querydsl?
The example is two separate or statements. I think the or should be nested, like this:
new BooleanBuilder(
qClaimDataEntity.companyId.eq(companyId)
.or(
qClaimDataEntity.projectDataEntity.remoteProjectDataEntityList
.any().assignedCompanyId.eq(companyId)
.or(
qClaimDataEntity.projectDataEntity.remoteProjectDataEntityList
.any().remoteProjectStatus.eq(RemoteProjectStatusEnum.合作中)
)
));

SELECT Vertex WITH multiple Edges

This is my schema:
My objective is capture that purple Vertex with name "Paragrafo" and have connection with the Vertex "1" AND "2", in this case will be the #29:426.
I tried:
SELECT EXPAND( BOTH( 'Contem' ) ) FROM Topico WHERE Assunto = '1' and Assunto = '2'
or
SELECT * FROM Topico WHERE Assunto = '1' and Assunto = '2'
or
SELECT FROM ( SELECT EXPAND(BOTHE('Contem')) FROM Topico WHERE Assunto='2' and Assunto = '1')
The AND operator make the query return None.
It's possible to make a query to do that?
I'm doing my schema right?
Thanks.
Try this:
select from Topico where out().<property_name> contains '1' AND out().<property_name> contains '2'
Hope it helps
Regards

Zend Db Select add subquery to FROM part

I try to build a query with subquery in FROM part using the Zend_Db_Select. Im looking for somthing like this:
SELECT COUNT(row_1) AS count_row FROM (SELECT row,row2,... FROM table WHERE row= ...) AS temp WHERE row = 0)
So I try to do it like this:
$oSubSelect =
$this->select()
->setIntegrityCheck(false)
->from('table',
array(
'row_id'
)
)
->where(PRFX.'table.id = '.PRFX.'table2.id')
->from(PRFX.'table2',array('row','row2'));
$this->select(false)
->setIntegrityCheck(false)
->from(new Zend_Db_Expr($oSubSelect).' AS temp',
array(
'COUNT(row_id) AS row_count',
)
);
But this gives me an error message.
Best regards.
Ok I fix this. The problem was in
->from(new Zend_Db_Expr($oSubSelect).' AS temp',
Should be:
->from(new Zend_Db_Expr('('.$oSubSelect.')'),

How can disable quote join Zend db

I've sql query
select * from table1
left join (values (4),(1800),(103500)) AS "filter (id) on table1.id=filter.id
By default Zend_Db_Select table quoted.
For example:
$result = '(values (4),(1800),(103500)) AS filter (id)';
$select->joinInner($result, "table1.id = filter.id", '');
result:
SELECT * FROM "table1"
INNER JOIN "(values (4),(1800),(103500)) filter (id)" ON table1.id=filter.id
Me need
SELECT * FROM "table1"
INNER JOIN (values (4),(1800),(103500)) filter (id) ON table1.id=filter.id
How can disable quote table?
Try adding $result to your $select as a Zend_Db_Expr.
This is a little tricky. Look at the code below.
$dbh = Zend_Db_Table::getDefaultAdapter();
$select = $dbh->select();
$select->from('table1');
$select->joinInner(
array('filter (id)' => new Zend_Db_Expr('(values (4),(1800),(103500))')),
"table1.id = filter.id",
array()
);
echo $select->assemble() . PHP_EOL;
This code by default outputs the following statement which is not what we really want because identifier filter (id) is quoted. Here is the output.
SELECT `table1`.* FROM `table1`
INNER JOIN (values (4),(1800),(103500)) AS `filter (id)` ON table1.id = filter.id
We need to disable autoQuoteIdentifiers in configuration options. For example:
'db' => array(
'adapter' => 'pdo_mysql',
'isDefaultTableAdapter' => true,
'params' => array(
'host' => '<host>',
'username' => '<user>',
'password' => '<pass>',
'dbname' => '<db>',
'options' => array(
'autoQuoteIdentifiers' => false,
),
),
)
We get the following output
SELECT table1.* FROM table1
INNER JOIN (values (4),(1800),(103500)) AS filter (id) ON table1.id = filter.id
Note that in this case developer is responsible for quoting the identifiers when needed.
I think it's impossible to selectively disable quoting for one of the table alias. Well at least I found this impossible when reviewed 1.x Zend Framework code I have here locally ;)

Zend_Db_Select: LEFT JOIN on a subselect

I have a query, that does a LEFT JOIN on a subselect. This query is run in a high load environment and performs within the set requirements. The query (highly simplified) looks like:
SELECT
table_A.pKey
, table_A.uKey
, table_A.aaa
, table_B.bbb
, alias_C.ccc
, alias_C.ddd
FROM table_A
INNER JOIN table_B ON table_A.pKey = table_B.pKey
LEFT JOIN (
SELECT
table_X.pKey
, table_X.ccc
, table_Y.ddd
FROM table_X
INNER JOIN table_Y ON table_X.pKey = table_Y.pKey
) AS alias_C ON table_A.uKey = alias_C.pKey;
(for various reasons, it is not possible to rewrite the subselect as a (direct) LEFT JOIN).
Now, I cannot get the LEFT JOIN on subselect to work with Zend_Db_Select. I've tried everything I could come up with, but it does not work.
So my question is:
Is it not possible to do a query as described above with Zend_Db_Select?
What syntax do I need to get it to work within Zend Framework?
I think that it should work like this:
$subselect = $db->select->from(array('x' => 'table_X'), array('x.pKey', 'x.ccc', 'y.ddd'), 'dbname')
->join(array('Y' => 'table_Y'), 'x.pkey = y.pkey', array(), 'dbname');
$select = $db->select->from(array('a' => 'table_A'), array(/*needed columns*/), 'dbname')
->join(array('b' => 'table_B'), 'a.pkey = b.pkey', array(), 'dbname')
->joinLeft(array('c' => new Zend_Db_Expr('('.$subselect.')'), 'c.pkey = a.ukey', array())
I haven't tried it but I believe it'll work.
...
->joinLeft(array('c' => new Zend_Db_Expr('(' . $subselect->assemble() . ')'), 'c.pkey = a.ukey', array())