SQlite delete inner join - sqlite

I have read alot of other answers but cannot get this working!.
select * from invTypes
inner join invGroups on invtypes.groupID = invgroups.groupID
where invGroups.categoryID = 25;
I simply want to delete the record from invTypes if the category ID matches to 25
Would be very grateful of assistance
I have already tried:
delete from invTypes
inner join invGroups on invtypes.groupID = invgroups.groupID
where invGroups.categoryID = 25;
Gives error:
near "inner": syntax error:
DELETE FROM invTypes
WHERE groupID in ( SELECT groupID FROM invGroups INNER JOIN invTypes ON (invGroups.groupID = invTypes.groupID)
WHERE invGroups.categoryID = 25);
Gives:
ambiguous column name: groupID: DELETE FROM invTypes
WHERE groupID in ( SELECT groupID FROM invGroups INNER JOIN invTypes ON (invGroups.categoryID = invTypes.categoryID) WHERE invGroups.categoryID = 25);

you can use delete with a where clause
delete from invTypes
where groupId in ( select groupId from invGroups where CategoryID =25)

DELETE invTypes
FROM invTypes
INNER JOIN invGroups
ON invTypes.groupID = invGroups.groupID
WHERE invGroups.categoryID = 25;

Related

Transform Sql to QueryBuilder

I try to transform a sql query to querybuilder.
SELECT c.id, SUM(a.view_count), dt
FROM project_content c
JOIN project_content_analytic a ON a.content_id = c.id
RIGHT JOIN (SELECT c.id, DATE_FORMAT(a.created_at, '%d-%m-%Y') dt, MAX(a.created_at) max_date
FROM project_content c
JOIN project_content_analytic a ON a.content_id = c.id
GROUP BY dt, c.id) x
ON x.max_date = a.created_at and x.id = c.id
GROUP BY dt
ORDER BY `x`.`dt` ASC
I tried:
$qb->select('c.id')
->addSelect('COALESCE(SUM(a.viewCount),0) as viewCount')
->addSelect('DATE_FORMAT(a.createdAt, \'%d-%m-%Y\') as date')
->innerJoin('c.analytics', 'a')
->groupBy('c.id')
->addGroupBy('date')
->orderBy('a.createdAt', 'DESC');
The subquery:
$sub->select('cc.id')
->addSelect('MAX(aa.created_at) as maxDate')
->addSelect('DATE_FORMAT(aa.createdAt, \'%d-%m-%Y\') as ddate')
$sub->innerJoin('cc.analytics', 'aa')
->addGroupBy('ddate')
->orderBy('aa.createdAt', 'DESC');
But i don't know how to join the queries.
Thank you for your help.

left join with external query in query-builder

I need to create that query in query builder
SELECT * FROM table1 LEFT JOIN table2 ON (table1.parent_id = table2.id OR table1.id = table2.id) WHERE table2.id IS NULL;
I already've got
$er->createQueryBuilder('p')
->leftJoin('Bundle2:table2', 'n')
->where('p.parent = n.id')
->andWhere('p.id = n.id');
but don't know how to add external WHERE to query ?
Try this:
return $this->createQueryBuilder('t1')
->leftJoin('t1.table2', 't2')
->where('t1.parent = t2.id OR t1.id = t2.id')
->andWhere('t2.id IS NULL')
->getQuery()
->getResult();

Android Sqlite Multiple Joins with Alias does not return anything

I have this query:
Cursor res = db.rawQuery("SELECT t1.Id, t2.EmpId FROM table1 t1
LEFT JOIN table2 t2 ON t1.t2Id = t2.Id
LEFT JOIN table3 t3 ON t1.t3Id = t3.Id
WHERE t1.Id = 90909", null);
Now when I go
if(res.moveToFirst()){
do{
MyObject obj = new MyObject();
obj.Id = res.getInt(res.getColumnIndex("t1.Id"));
MyObjectAsProperty objProp = new MyObjectAsProperty();
objProp.EmpId = res.getInt(res.getColumnIndex("t2.EmpId"));
}while(res.moveToNext());
}
The objProp.EmpId returns nothing (or 0). I looked at the cursor columns and it's Fields basically did not include the table alias names. How do I go about getting the values of those aliased fields?
Thanks!
EDIT
Apologies. I forgot to add that if for example it's referencing the same other table multiple times with the same field.
Here's updated query
Cursor res = db.rawQuery("SELECT t1.Id, t2.EmpId, t2b.EmpId FROM table1 t1
LEFT JOIN table2 t2 ON t1.t2Id = t2.Id
LEFT JOIN table2 t2b ON t1.t2bId = t2b.Id
WHERE t1.Id = 90909", null);
if(res.moveToFirst()){
do{
MyObject obj = new MyObject();
obj.Id = res.getInt(res.getColumnIndex("t1.Id"));
MyObjectAsProperty objProp = new MyObjectAsProperty();
objProp.EmpId = res.getInt(res.getColumnIndex("t2.EmpId"));
MyObjectAsProperty objProp2 = new MyObjectAsProperty();
objProp2 .EmpId = res.getInt(res.getColumnIndex("t2bId.EmpId"));
}while(res.moveToNext());
}
The documentation says:
The name of a result column is the value of the "AS" clause for that column, if there is an AS clause. If there is no AS clause then the name of the column is unspecified and may change from one release of SQLite to the next.
So you must use AS:
SELECT t1.Id AS ObjID, t2.EmpId AS EmpId, ...

How to write the INNER JOIN in dql

This is my select and it works in workbench fine, but how did i write the INNER JOINS for doctrine?
$qb = $em->createQuery(
"SELECT oh.objektnummer, oh.idSubunternehmer, oh.datum, oh.typ, oh.outbox
FROM MBSAllgemeinBundle:ObjektHistory oh
INNER JOIN MBSAllgemeinBundle:Objekt o ON o.objektnummer = oh.objektnummer AND o.idSubunternehmer = oh.idSubunternehmer
INNER JOIN MBSAllgemeinBundle:Subunternehmer s ON s.subunternehmernummer = o.id_subunternehmer
INNER JOIN MBSAllgemeinBundle:SubunternehmerUser su ON su.id_subunternehmer = s.subunternehmernummer
WHERE su.idUser = 1"
);
Try this syntax in your repository :
return $this->createQueryBuilder('oh')
->select('partial oh.{id, objektnummer, idSubunternehmer, datum, typ, outbox}')
->innerJoin('MBSAllgemeinBundle:Objekt', 'o', 'ON', 'o.objektnummer = oh.objektnummer AND o.idSubunternehmer = oh.idSubunternehmer')
->addSelect('o')
// other INNER JOIN
// other INNER JOIN
->where('su.idUser = :id')
->setParameter('id', 1)
->getQuery()
->getResult()
;

Sql select statement selecting 4 value from same table

Hello everyone I have created two select statements but I want to merge them together to give one select statement that display only 4 record can someone help please?
SELECT
DATENAME(month, BID.Date) AS Years,
COUNT(DATENAME(Month, BID.Date)) AS Total
FROM
Auction
INNER JOIN
BID ON Auction.AuctionID = BID.AuctionID
INNER JOIN
Item ON Auction.ItemID = Item.ItemID
WHERE
(Auction.Status = 'Expired')
AND (Item.SellerID = #seller)
AND (BID.Status = 'Won')
GROUP BY
DATENAME(month, BID.Date)
SELECT
DATENAME(month, BID.Date) AS Years1,
COUNT(DATENAME(Month, BID.Date)) AS Total1
FROM
Auction
INNER JOIN
BID ON Auction.AuctionID = BID.AuctionID
INNER JOIN
Item ON Auction.ItemID = Item.ItemID
WHERE
(Auction.Status = 'UnSold')
AND (Item.SellerID = #seller)
GROUP BY
DATENAME(month, BID.Date)
SELECT
DATENAME(month, BID.Date) AS Years
,COUNT(CASE WHEN Auction.Status = 'Expired'
AND Item.SellerID = #seller
AND BID.Status = 'Won'
THEN DATENAME(Month, BID.Date) ELSE NULL END) AS Total1
,COUNT(CASE WHEN Auction.Status = 'UnSold'
AND Item.SellerID = #seller
THEN DATENAME(Month, BID.Date) ELSE NULL END) AS Total2
FROM Auction INNER JOIN BID
ON Auction.AuctionID = BID.AuctionID
INNER JOIN Item
ON Auction.ItemID = Item.ItemID
GROUP BY DATENAME(month, BID.Date)

Resources