Typeorm subquery add select - typeorm

I am new for using typeorm and this is the second time I am confused with typeorm, I have the following query :
SELECT t1.a,t1.b,t2.a
(SELECT TOP 1 t1.a
FROM table1 t1
WHERE t1.b = t2.a
ORDER BY t1.a DESC
) AS MaxT1
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.a = t2.a
I tried this:
let query = await getManager()
.createQueryBuilder(Bid, 'bid')
.select([
'l.ID_anv_Lot',
'l.LotNumber',
'w.WineryName',
'bid.BidAmount',
'bid.ProxyBidAmount',
'er.ID_Contact'
])
.addSelect(Table1, t1)
.innerJoin(Lot, 'l', 'l.lotNumber = bid.lotNum AND l.paddleNumber = bid.paddleNumber')
but the result is all of the rows on table1

This Example may help you to perform sub query execution:
const posts = await connection.getRepository(Post)
.createQueryBuilder("post")
.where(qb => {
const subQuery = qb.subQuery()
.select("usr.name")
.from(User, "usr")
.where("usr.registered = :registered")
.getQuery();
return "post.title IN " + subQuery;
})
.setParameter("registered", true)
.orderBy("post.id")
.getMany();

You can use subselects in SELECT statements:
let query = await this.createQueryBuilder('t1')
.select()
.innnerJoin('t1.t2', 't2', 't1.a = t2.a')
.addSelect(subQuery => {
return subQuery
.select('_t1.a')
.from(Table1, '_t1')
.where('_t1.b = t2.a');
}, 'MaxT1')
.getRawMany()
You can find more here: https://orkhan.gitbook.io/typeorm/docs/select-query-builder

Related

How to translate an raw SQL query to TypeORM query builder?

How would I convert the subQuery select with distinct to TypeORM querybuilder? Thanks.
SELECT `time`, (case when `start` is NULL then 0 else 1 end) `is_reserved` FROM a left join (SELECT `time` FROM b join c on c.b_id = b.id WHERE b.date = '2022-04-20') MySchedule on MySchedule.time = a.time where a.is_use = 1
a table
time
is_use
b table
id
date
c table
b_id
time
You can first build the nested query and then left-join it to the main query:
import { getConnection } from 'typeorm';
// build the nested query
const nestedQuery = getConnection()
.createQueryBuilder(B, 'b').select('time')
.innerJoin(C, 'c.b_id = b.id')
.where(`b.date = '2022-04-20'`);
getConnection().createQueryBuilder(A, 'a')
.select([
'time',
])
.addSelect('(case when `start` is NULL then 0 else 1 end)', 'is_reserved')
// left join the nested query
.leftJoin(
`(${nestedQuery.getQuery()})`,
'MySchedule',
'MySchedule.time = a.time')
.where('a.is_use = 1')
.getMany();
Where A, B and C are TypeORM entities.

SQL Raw data query

Hi I am trying to query some data like this
$sql = "SELECT *
FROM company";
$stmt = $this->getDbAdapter()->query($sql, \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
if ($stmt instanceof \Zend\Db\ResultSet\ResultSet) {
foreach ($stmt as $row) {
$entities[] =$row;
}
}
But I get an 500 Internal server error, Is there something wrong with this code?
I have done this one an it work fine.
$sql = "SELECT company.*,address_home.*,address_billing.*, T1.*
FROM company AS company
INNER JOIN address AS address_home ON company.address_id_fk = address_home.address_id
INNER JOIN address AS address_billing ON company.billing_address_id_fk = address_billing.address_id
INNER JOIN (SELECT company_id_fk,
max(case when company_role_id_fk = '1' then 'true' end) isClient,
max(case when company_role_id_fk = '2' then 'true' end) isSupplier
FROM company_role_company_maps AS crcm
WHERE crcm.company_id_fk = $id
GROUP BY company_id_fk) AS T1
ON(company.company_id = T1.company_id_fk)
WHERE company.company_id = $id";
$resultset = $this->getDbAdapter()->query($sql, \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
$current = $resultset->current()->getArrayCopy();
But for some reason when I do the loop i get the error.
I try this to see If I cud catch something.
$sql = "SELECT *
FROM company";
$stmt = $this->getDbAdapter()->query($sql, \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
if ($stmt instanceof \Zend\Db\ResultSet\ResultSet) {
try{
foreach($stmt as $row) {
var_dump($row);
}
}catch(\Exception $e){
var_dump($e->getMessage());
}
}
But I just got the Internal server error message (There is a problem with the resource you are looking for, and it cannot be displayed.)

How do you use the Group Statement on your SQL to Linq queries?

I would like to end up with a list where my categories are grouped according to the users id.
IEnumerable<JoinClass> catList =
from c in db.Users2
join e in db.Categories on c.Id_Users equals e.FK_Users
where c.EEID == UserEEID
group e.Category by c.EEID in z
select new JoinClass
{
Category = e.Category,
EEID = c.EEID,
};
return View(catList.ToList() );
IEnumerable<JoinClass> catList =
from c in db.Users2
join e in db.Categories on c.Id_Users equals e.FK_Users
where c.EEID == UserEEID
group e.Category by c.EEID in z
select new JoinClass
{
Categories = z.ToList(),
EEID = z.Key,
};
return View(catList.ToList() );
Assuming you ACTUALLY meant to find all Categories for each EEID.

Zend Framework 2 Sql Select with OR and AND

I want to make this query using Zend\Db\Sql\Select:
SELECT table1.* FROM table1
INNER JOIN table2 ON table1.columnA = table2.columnB
INNER JOIN table3 ON table1.columnC = table3.columnD
WHERE (table2.column2 = 2 or table3.column3 = 3) and table1.column1 = 1
ORDER BY table1.columnE ASC LIMIT 1
I have this code so far:
/*#var $db Adapter */
$db = $this->getServiceLocator()->get('db');
$sql = new Sql($db);
$select = $sql->select();
$select->from('table1');
$select->join('table2','table1.columnA = table2.columnB',array());
$select->join('table3','table1.columnC = table3.columnD',array());
$select->where(array('table2.column2' => 2, 'table2.column3' => 3), Predicate\PredicateSet::OP_OR);
$select->where(array('table1.column1' => 1),Predicate\PredicateSet::OP_AND);
$select->order('table1.columnE ASC');
$select->limit(1);
$statement = $sql->prepareStatementForSqlObject($select);
$resultSet = $statement->execute();
But doesn't works, because produce this one (without the "(" and ")" for the OR):
SELECT table1.* FROM table1
INNER JOIN table2 ON table1.columnA = table2.columnB
INNER JOIN table3 ON table1.columnC = table3.columnD
WHERE table2.column2 = 2 or table3.column3 = 3 and table1.column1 = 1
ORDER BY table1.columnE ASC LIMIT 1
What can I do?
from the top of the head using Where fluent interface:
$select->where
->nest
->equalTo('table2.column2', 2)
->or
->equalTo('table2.column3', 3)
->unnest
->and
->equalTo('table1.column1', 1);
I would do something like:
$where = new \Zend\Db\Sql\Where();
$where
->nest()
->equalTo('table2.column2', 2)
->or
->equalTo('table2.column3', 3)
->unnest()
->and
->equalTo('table1.column1', 1);
$select->where($where)
Just because this way your $select keep being an implementation of Zend\Db\Sql\SqlInterface
while doing
$select->where
->nest
will return an instance of a Zend Sql operator. Which is not bad but then you can't just do
$statement = $sql->prepareStatementForSqlObject($select);
$resultSet = $statement->execute();

Nested query in Linq

I have three tables. I have to retrieve the data using Linq statement. My three table names are A,B,C. I connected join for connecting two tables A and B based on the id's like:
select ol, fN, LN, ci, co
from member
join details
on member_id = details_id
where details_id in
(select contacts_id from contacts where
contacts_id1 = 1 and contacts_usr_id = 1)
I am able to write the query up to the where condition, how can I write the query for the inner while condition?
you can try this:
var idlist = (from tbl in table3
where tbl.usr_id == 1 && tbl.contacts_id == 1
select tbl.contacts_id ).ToList();
var x = from A in table1
from B in table2 where A.user_id == B.user_id
&& idlist.Contains(A.user_id)
select new { a = A.a, b = A.b, c = A.c, d = B.d, e = B.e };
check and let me know if that work.
Try flipping the query upside down. How about the following:
var query =
from t3 in table3
where t3.user_id = 1 && t3.contacts_id = 1
join t2 in table2 on t3.contacts_id equals t2.usr_id
join t1 in table1 on t2.usr_id equals t1.userid
select new {t2.a, t2.b, t2.c, t1.d, t1.e};

Resources