I Have Two Tables , Table1 Contains 10 Users Regular Data, Table2 Contains 3 vip users username(same as table1) only . Now how can I Get All 7 Non-VIP Users data using sql? Platform IS sqlite Database Android
Somnath,
It would be great if you would provide the database platform and actual table schemas in your question. Not having this information requires people to make assumptions. Anyway, I think it would like like this.
select *
from table1
where not exists(select * from table2 where table2.username = table1.username)
Something like:
SELECT * from Table1
WHERE user_id
NOT IN (SELECT user_id from Table2);
Translating to: Select everything in Table 1 where user_id doesn't exist in list of user_id's from Table2.
Related
I have ids in my table, ids start from 1 to 20, I want a query, to find the first and last records in a given table but I want the result by some condition.
For example: if I have the record
1,2,3,4,5,9,10,11,12,13, 19,20
I need a result like 1-5, 9-13, 19-20 like this I need results
This is the island part of the classic gaps and islands problem (With the gaps part being finding the missing values in between each island). If you search for that term, you'll find a ton of material about how to calculate them.
One approach (Requires Sqlite 3.25 or newer for window function support):
sqlite> CREATE TABLE ex(id INTEGER PRIMARY KEY);
sqlite> INSERT INTO ex VALUES (1),(2),(3),(4),(5),(9),(10),(11),(12),(13),(19),(20);
sqlite> WITH cte AS (SELECT id, id - row_number() OVER (ORDER BY id) AS grp FROM ex)
...> SELECT min(id) AS rangestart, max(id) AS rangeend FROM cte GROUP BY grp;
rangestart rangeend
---------- ----------
1 5
9 13
19 20
SQL Query to find first record in your table:
SELECT * FROM <table_name> ORDER BY <column_name> ASC LIMIT 1
SQL Query to find last record in your table:
SELECT * FROM <table_name> ORDER BY <column_name> DESC LIMIT 1
For example: if I have the record 1,2,3,4,5,9,10,11,12,13, 19,20
I need a result like 1-5, 9-13, 19-20 like this I need results
If you need result like you have mentioned, then you can set LIMIT in your query to get how many records you can have in that query.
QUERY:
SELECT * FROM <table_name> LIMIT <any_number>
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
I'm super-novice at Android programming and have this project I'm stuck on.
I have a database of (conlang) dictionaries where each language is in its own table. The user searches these and the results will display in a ListView. Right now, I've got it working to search only one table.
I want it to be able to search all three tables and display the results in one ListView with rows from each of the tables. I'd like to indicate which table each row came from. The results would display something like:
From language
Lang Term : English term
Additional information
I've set up a CursorAdapter and have this currently working for one of the three tables.
...
Since the dictionaries came from different sources, they have different information. As an example:
One has the columns EngTerm, VLangTerm, FullDefinition
Another has EngTerm, RLangTerm, PartOfSpeech
And the other has EngTerm, GLangTerm, PartOfSpeech, and PronounciationGuide (possibly).
For example, table1 might contain results for "land," "landing gear" and "landscape" and table2 might contain "land," "landing" and "island" and table3 might contain "bland" and "homeland."
The user can search these tables right now only by the english term, and can select to match by full word or partial word.
Because of how they are structured, I'm guessing there is no way to do a JOIN query.
I don't know if this helps at all, but right now, I've got this method in my SQLiteOpenHelper class:
public Cursor searchDictionaryByEnglish
(boolean matchFullWordOnly, String searchTerm) {
String sql = "SELECT * FROM " + TABLE_VULCAN +
" WHERE " + COLUMN_ALL_ENG_TERM + " MATCH ?";
if (!matchFullWordOnly)
searchTerm = "*" + searchTerm + "*";
return mDataBase.rawQuery(sql, new String[]{searchTerm});
}
(It is an FTS3 table it's searching, thus the MATCH rather than LIKE.)
I hope I've made it clear what I'm trying to accomplish, but I'm not sure where to start. I've found some answers about MergeCursor but I don't know how to apply that because of the different number and names of columns.
Here's how to do a union if you don't have one of the columns in one of the tables, it will work if you're only searching through the common column, that all the tables have.
SELECT field1 ,
field2 ,
field3
FROM ( SELECT field1 ,
field2 ,
field3
FROM table1
UNION
SELECT field1 ,
field2 ,
field3
FROM table2
UNION
SELECT field1 ,
field2 ,
NULL AS field3
FROM table3
) tbl
WHERE tbl.field1 LIKE '%search_string%'
Note the NULL AS field3 in the last UNION.
Create a Adapter that does accept the fields you require to display.
This may be a simple question as I have not done any database work for a while!
I have two tables with data like the below
Table 1
Rows with Primary Keys 1,2
Table 2
Rows with Foreign keys 1,2,3,4
I was to be able to perform a DELETE statement which will remove all rows from Table 2 that do not have a corresponding primary key in table 1, which in this case would result in only rows with foreign keys 1 & 2 being left in the table.
I should mention that this is on Android so I am using SQLite and also I am interested in the ease of doing this via a content provider.
Thanks for any help
Try this:
String SQL="DELETE FROM Table2
WHERE (Table2.FQ1,Table2.FQ2) NOT IN (SELECT PK1,PK2 FROM Table1)";
db.SQL(SQL);
But i'm not sure that the (Table2.FQ1,Table2.FQ2) sentence will run into the NOT IN
Suppose I have two database files a.sqlite and other one is b.sqlite. suppose table1 is in a.sqlite and table2 in b.sqlite. I open a.sqlite in read-only mode and b.sqlite in read-write.Suppose both table table1 and table2 has same column name "description".table2 has description column with all null values and table1 has some values.So how can i add the data from table1 into table2.I know through query.But as these r in two different databse,so is there ant problem? Can any one suggest ?
Not sure if your environment allows it, but ATTACH DATABASE is normally the way to go:
http://www.sqlite.org/lang_attach.html