sqlite delete statement with JOIN an UNION - android

I have to do a complicated delete-statement, that looks like this:
DELETE FROM
(Select t1._id, t1.subject, t1.predicate, t1.object from t1
INNER JOIN t1 AS t2 ON t1.subject=t2.subject
WHERE t2.object='ID20447_325762212'
UNION
Select t1._id, t11.subject, t1.predicate, t1.object from t1
INNER JOIN t1 AS t2 ON t1.object=t2.subject
WHERE t2.object='ID20447_325762212')
SQLitedabatase#delete(Table, selection, selectionArgs) is not useable I think. No way to have Joins and Unions.
SQLiteQueryBuilder has JOINs with setTables(joinString) and UNIONs with subQuery() and Unionquery, but only for selectqueries not delete.
How does it work?
[update]
I could reduce complexity by separating both subqueries in two delete-statements, but there is a join in everyone so thats not a solution. And that's not working either.
[update2]
Here's a screenshot from rows of my table i want to delete.
In my app I have an object with ID...2328 (row 2) and a property(relation) represented by the rows in that screenshot. The relation is targeting to an object with id ...212 . I have to delete that object and all Properties

You can use complex selections in subqueries.
In the general case, it would look like this:
DELETE FROM SomeTable WHERE _id IN (SELECT _id FROM ...complex query...)
However, this particular query isn't very complex after all:
DELETE FROM t1
WHERE subject = (SELECT subject FROM t1 WHERE object = 'ID20447_325762212')
OR object = (SELECT subject FROM t1 WHERE object = 'ID20447_325762212')

Related

Get Wordpress posts with images link

I want to get wordpress posts with specific category and link of images.
As you know images links save to database in guid column, when post_type = attachment.
and ID of post and post_parent are the same.
Now I want to get posts and join guid column to same ID.
When I added Inner join to combine attachment and post, I got error!
Please help me, if you know the way that I can get post with specific category and images link of each post.
Here is my code:
SELECT
*
FROM
wp_posts p,
wp_postmeta m,
wp_terms t,
wp_term_taxonomy tt,
wp_term_relationships tr,
wp_terms t2,
wp_term_taxonomy tt2,
wp_term_relationships tr2
LEFT JOIN wp_posts AS p2
ON
p.ID = p2.post_parent
WHERE
p.post_type = 'post' AND p.post_status = 'publish'
AND p.ID = tr.object_id
AND t.term_id = tt.term_id
AND tr.term_taxonomy_id = tt.term_taxonomy_id
AND tt.taxonomy = 'category'
AND tt.term_id = t.term_id
AND t.name = 'Fashion'
GROUP BY
p.ID
ORDER BY
id
DESC
MySQL said:
#1054 - Unknown column 'p.ID' in 'on clause'
I suspect that the problem is due to mixing the old school comma syntax with the newer JOIN keyword.
Relevant excerpt from MySQL Reference Manual:
INNER JOIN and , (comma) are semantically equivalent in the absence of a join condition: both produce a Cartesian product between the specified tables (that is, each and every row in the first table is joined to each and every row in the second table).
However, the precedence of the comma operator is less than that of INNER JOIN, CROSS JOIN, LEFT JOIN, and so on. If you mix comma joins with the other join types when there is a join condition, an error of the form Unknown column 'col_name' in 'on clause' may occur. Information about dealing with this problem is given later in this section.
The easiest way to avoid this problem is to ditch the old school syntax for the join operation, use the JOIN keyword instead.
(It's great that the comma syntax is still valid, to provide backwards compatibility with existing SQL. But there's no good reason new development should use the comma syntax.)
Aside from that, there's a couple of big rock issues that stick out to me.
Seems like there's a lot of join conditions missing
Using * for the SELECT list in development can be useful shortcut, but we usually list the expressions we need to return, especially if we want to return id column from multiple tables, where we like to assign a column alias to avid duplicate columns names.
Relying on the non-standard extension to GROUP BY (when only_full_group_by is omitted from sql_mode to return values from "some" row in the collapsed group
Those all look like serious problems to me.
We can re-write the OP query to use JOIN keyword in place of comma syntax, and relocating conditions to the ON clause, this highlights what looks like missing join conditions:
SELECT *
FROM wp_posts p
JOIN wp_postmeta m
-- ON ???
JOIN wp_terms t
ON t.name = 'Fashion'
JOIN wp_term_taxonomy tt
ON tt.term_id = t.term_id
AND tt.taxonomy = 'category'
JOIN wp_term_relationships tr
ON tr.object_id = p.id
AND tr.term_taxonomy_id = tt.term_taxonomy_id
JOIN wp_terms t2
-- ON ???
JOIN wp_term_taxonomy tt2
-- ON ???
JOIN wp_term_relationships tr2
-- ON ??
LEFT
JOIN wp_posts AS p2
ON p2.post_parent = p.id
WHERE p.post_type = 'post'
AND p.post_status = 'publish'
GROUP
BY p.id
ORDER
BY p.id DESC
Where we are going to omit any join condition, and just match all rows to all other rows, then my preference is to include the (optional) CROSS keyword, as an aid the future reader, to signal that the omission of a join condition is by design, and not an oversight.

Android : sqlite join result

I have a table which has one column's name and guestof .
I want to return a cursor to get three values : name guestOf and guestcount where guestcount is how many rows has "name" as the value of guestOf.
How can combine this internal query and return the result. Trying something like this, can it be done without inner query for better performance
SELECT name, guestCount from Table
as A outer join (SELECT guestOf , count(*)
AS GuestCount FROM Table group by guestOf)
as GuestCounts WHERE A.id = GuestCounts.guestOf
Give this code a go. I think it matches what you're looking for:
SELECT g.name, g.guestof,
(SELECT COUNT(guestof)
FROM guests AS c
WHERE g.name == c.guestof
) as guestcount
FROM guests AS g;
And as an example: SQLFiddle

How can i join two tables and return rows with does not match. In sqlite

I have Two Tables Table1 and Table 2 with 3 columns id,name and mac address and in second table I also have column named mac.
The things I need that in table 1 I have 10 records and in table 2 I have only 5 records and I want the records which are not present in table2.
How can I do this in sqlite android? Any help would be appreciated. Thanks in advance...
Thanks for making a time to read this
In Table name I am adding a name from second class
this query is for sql and i need for sqlite
SELECT T1.*
FROM T1
WHERE NOT EXISTS(SELECT NULL
FROM T2
WHERE T1.ID = T2.ID
AND T1.Date = T2.Date
AND T1.Hour = T2.Hour)
This should work with SQLite, albeit not efficiently.
If ID does what it should do, then checking for T1.Date = T2.Date and T1.Hour = T2.Hour could be left out.
Better way using JOIN:
SELECT T1.*
FROM T1
LEFT JOIN T2 ON T1.ID = T2.ID
WHERE T2.ID IS NULL;
you specify the condition based on reference key (I assumed MAC)
Select T1.ID, T1.Name From Table1 T1 LEFT JOIN From Table2 T2 on T1.Mac=T2.Mac where T2.Mac is NULL.
or
SELECT id, name
FROM Table2
WHERE NOT EXISTS
(SELECT *
FROM Table2
WHERE Table1.Mac = Table2.Mac)
SELECT *
FROM T1
WHERE T1.name NOT IN (SELECT name FROM T2 )
-------------------Or do this for more restriction--------------------------
SELECT *
FROM T1
WHERE T1.name NOT IN (SELECT name FROM T2 ) and
T1.mac NOT IN (SELECT mac FROM T2 )

joining two table in sqlite

i have two table
and table two data is
and i want to show just like this
I have to make a table out of two other tables one table with column word and serial and another with column meaning
and serial.
Now what I have to do is put this result(data)
into table3 (a table i already have, with the same columns as in table1 and (table2) tried lots but can't please help me
I hope this isn't too confusing of a question.
select
a.serial,
b.word,
a.word as meaning
from tbl1 a
join tbl2 b on a.serial = b.serial
This is the sql query your looking for ,details related to the same is been provided in the link below
INSERT INTO table3 (serial,word,meaning)
SELECT t1.serial,t2.word,t1.word
FROM table1 as t1
JOIN table2 as t2 ON t1.serial=t2.serial
click here w3schools SQL tutorial
Try
select * from eng join other on eng.serial=other.serial

Sqlite - query 1 column in two identical tables where column equals a value

I have two identical tables (month1, month2) and I am trying to find all records from both tables where task1_done = 1. I want the last row in that set (i move cursor to last for this). I have played with inner outer natural joins but can't seem to get month2 values. Here is what I have:
String query = "SELECT m1.columnA, m1.columnB, m1.columnC, m1.columnD, m1.columnE, m1.columnF FROM month1 m1, month2 m2 WHERE m1.task1_done = 1 OR m2.task1_done = 1";
Any help would be great!
I think you want a union all for this query:
select m.*
from (select *
from months1
union all
select *
from months2
) m
where task1_done = 1;
Note: I have used * as a convenience because you said the two tables have the same structure. You should actually list the columns that you want from the two tables.
In general, having two tables with the same layout is a sign of a bad database design. It is usually better to have a bigger table, with another column identifying "month1" or "month2".
EDIT:
SQL tables do not have a "last" value. If you have a an id or timestamp column that you can use for ordering, then you can do:
select m.*
from (select *
from months1
union all
select *
from months2
) m
where task1_done = 1
order by id desc
limit 1;
Are these tables related or have any references? if not you can have separate statement and do a union
i.e.
select top 1 column1, column2.. from month1 WHERE task1_done = 1 order by IdentityColumn Desc
union
select top 1 column1, column2.. from month2 WHERE task1_done = 1 order by IdentityColumn Desc

Categories

Resources