SQLite: How do I search multiple values? - android

I have two autocomplettextview and 3 columns in my table col1,col2,col3. col1 is primary key
table look like this:
| col1 | col2 | col3 |
-----------------------------------------
| 1 | store1 |apple,pineapple,mango |
| 2 | store2 | apple,orange |
| 3 | store3 | apple,pineapple,orange|
| 4 | store4 | orange,mango |
| 5 | store5 | mango,jackfruit |
I have two questions:
Q1: can i store multiple values in single column.If No then how can I store?
Q2: previously said i have two autocompletetextview to search
I.e.:
If user types apple and pineapple in two autocompletetextview then I need to
display row that matches these two texts.
Expected o/p:
| col1 | col2 | col3 |
-----------------------------------------
| 1 | store1 |apple,pineapple,mango |
| 2 | store3 | apple,pineapple,orange|
how can I achieve this?
I gone through several reseaches, with the help of foreign key I can store multiple values in single column, but how can I use foreign key in Where clause to achieve expected o/p.

You shouldnt store multiple values in same column. Instead you should use three tables.
Then is easy to find what StoreID have both food
SELECT FoodAvailable.StoreID
FROM FoodAvailable
JOIN Foods
ON FoodAvailable.FoodID = Foods.FoodID
WHERE Foods.Food in ('apple', 'pineapple')
GROUP BY FoodAvailable.StoreID
HAVING count(*) = 2
Store
| StoreID | StoreName |
-----------------------
| 1 | store1 |
| 2 | store2 |
| 3 | store3 |
| 4 | store4 |
| 5 | store5 |
Food
| FoodID | Food |
-----------------------
| 1 | apple |
| 2 | pineapple |
| 3 | mango |
| 4 | orange |
| 5 | jackfruit |
FoodAvailable
| StoreID | FoodID |
---------------------
| 1 | 1 | //apple
| 1 | 2 | //pineapple
| 1 | 3 | //mango
| 2 | 1 | //apple
| 2 | 4 | //orange
| 3 | 1 | //apple
| 3 | 2 | //pineapple
| 3 | 4 | //orange
| 4 | 4 | //orange
| 4 | 3 | //mango
| 5 | 3 | //mango
| 5 | 5 | //jackfruit
Your Final query is something like this
SELECT f.StoreID, GROUP_CONCAT(food ORDER BY food DESC SEPARATOR ', ')
FROM FoodAvailable f
WHERE f.StoreID = (SELECT FoodAvailable.StoreID
FROM FoodAvailable
JOIN Foods
ON FoodAvailable.FoodID = Foods.FoodID
WHERE Foods.Food in ('apple', 'pineapple')
GROUP BY FoodAvailable.StoreID
HAVING count(*) = 2
)
GROUP BY f.StoreID

Related

SQLite Android JOIN query

I want to fetch the records from SQLite database from 3 Tables.
Tables Structure
1. tb_contacts Table
It contains the contact information like id, name and email.
contact_id | contact_name | contact_email
---------- | ------------ | ------------------
1 | Deepak | deepak#yopmail.com
---------- | ------------ | ------------------
2 | Shivam | shivam#yopmail.com
2. tb_numbers Table
It contains the phone number of each contact
id | contact_id | contact_phone
---| ---------- | ------------
1 | 1 | 123456789
---| ---------- | ------------
2 | 1 | 789456123
---| ---------- | ------------
3 | 2 | 456123654
---| ---------- | ------------
4 | 2 | 698521473
3. tb_deleted table status
this table conatins the status of contact whether it is deleted or not with user_id. If this table contains the row of contact with particular user_id, it means the contact has deleted for that user, otherwise not.
id | contact_id | user_id
---| ---------- | --------
1 | 1 | 22201
---| ---------- | --------
2 | 1 | 22202
---| ---------- | --------
3 | 2 | 22201
What i want
1. Suppose user with user_id 22201. It will get all the contacts like
contact_id | contact_name | contact_email | contact_phone | deleted
---------- | ------------ | ------------------ | ------------- | -------
1 | Deepak | deepak#yopmail.com | 123456789 | 1
---------- | ------------ | ------------------ | ------------- | -------
1 | Deepak | deepak#yopmail.com | 789456123 | 1
---------- | ------------ | ------------------ | ------------- | -------
2 | Shivam | shivam#yopmail.com | 456123654 | 1
---------- | ------------ | ------------------ | ------------- | -------
2 | Shivam | shivam#yopmail.com | 698521473 | 1
2. Now user with user_id 22202.
contact_id | contact_name | contact_email | contact_phone | deleted
---------- | ------------ | ------------------ | ------------- | -------
1 | Deepak | deepak#yopmail.com | 123456789 | 1
---------- | ------------ | ------------------ | ------------- | -------
1 | Deepak | deepak#yopmail.com | 789456123 | 1
---------- | ------------ | ------------------ | ------------- | -------
2 | Shivam | shivam#yopmail.com | 456123654 | 0
---------- | ------------ | ------------------ | ------------- | -------
2 | Shivam | shivam#yopmail.com | 698521473 | 0
What i have tried
SELECT tb_contacts.contact_id, tb_contacts.contact_name, tb_contacts.contact_email, tb_numbers.contact_phone from tb_contacts join tb_numbers on tb_contacts.contact_id = tb_numbers.contact_id left join tb_deleted on tb_contacts.contact_id = tb_deleted.contact_id;
It is providing me the records of each contact with deleted status but without user_id. How can i get the required data with condition of user_id. I have used WHERE clause but with this, it only provide the records of those contacts for which there is a entry in tb_deleted table.
What is the best way or solution so that i can get the required data.
Thanks.
To look up whether the user is deleted, use a correlated subquery:
SELECT contact_id,
contact_name,
contact_email,
contact_phone,
EXISTS (SELECT *
FROM tb_deleted
WHERE contact_id = tb_contacts.contact_id
AND user_id = 22201
) AS deleted
FROM tb_contacts
LEFT JOIN tb_numbers USING (contact_id);
try with this query:
SELECT tb_contacts.*, contact_phone, deleted FROM(
SELECT tb_numbers.contact_id, tb_numbers.contact_phone, COUNT(tb_deleted2.user_id) as deleted FROM tb_numbers
CROSS JOIN (SELECT DISTINCT tb_deleted.user_id FROM tb_deleted) as tb_deleted
LEFT JOIN tb_deleted as tb_deleted2 ON tb_numbers.contact_id = tb_deleted2.contact_id and
tb_deleted.user_id = tb_deleted2.user_id AND tb_deleted2.user_id = '22201'
group by tb_numbers.contact_id, tb_numbers.contact_phone) as crossed_join
LEFT JOIN tb_contacts ON tb_contacts.contact_id = crossed_join.contact_id
Result:
using cross join might slow things down, you can try using the SELECT ..., (Select COUNT(*) FROM tb_deleted WHERE contact_id = deleted_contact_id and deleted_user_id = '2201') as deleted FROM ..... not sure if the speed is faster or slower though
You can try doing a query like this:
SELECT tb_contacts.contact_id, tb_contacts.contact_name, tb_contacts.contact_email, tb_numbers.contact_phone,
FROM tb_contacts
JOIN tb_numbers ON tb_contacts.contact_id = tb_numbers.contact_id
WHERE tb_contacts.contact_id NOT IN (
SELECT tb_deleted.contact_id from tb_deleted WHERE tb_deleted.user_id = user_id
);

Android Database Query Cross-row Check

I am working on a project which requires me to do some query in android database. However, I am completely new to database and I am a little confused at how to deal with such scenarios. Here's a rough example to show my question.
For a table like this:
TableA:
--------------------------------------------------------
| _id | name | mimetype | data1 | data2 | data3 |
--------------------------------------------------------
| 1 | user1 | mime1 | val1 | val2 | x |
--------------------------------------------------------
| 2 | user1 | mime2 | val3 | val4 | y |
--------------------------------------------------------
| 3 | user1 | mime3 | val8 | val5 | a |
--------------------------------------------------------
| 4 | user2 | mime2 | val6 | val7 | q |
--------------------------------------------------------
| 5 | user2 | mime3 | val9 | val10 | a |
--------------------------------------------------------
| 6 | user3 | mime1 | val11 | val12 | b |
--------------------------------------------------------
Basically, I want to do a query to return data from columns of name, data1, data2 from TABLE A when it fits the following criteria:
mimetype = mime2
there's another row in the table that has the same name and its mimetype = mime3 and data3 = a
I am wondering how to do this in one query.
If anyone can give some direction on how to figure this out, it will be great! Thanks!
This can be done with the EXISTS operator and a correlated subquery:
SELECT ...
FROM TableA
WHERE mimetype = 'mime2'
AND EXISTS (SELECT 1
FROM TableA AS T2
WHERE T2.name = TableA.name
AND T2.mimetype = 'mime3'
AND T2.data3 = 'a');

Find difference of sum of column for different users

I want to find out difference of sum of debit and credit transactions of user and list them out.
Here is the query which i have tried out:
SELECT *, (val1 - val2)
FROM (SELECT *, sum(transactionAmount) AS val1
FROM tableTransaction
WHERE creditedToNum = My_Number
GROUP BY debtToNum) db
JOIN (SELECT *, sum(transactionAmount) AS val2
FROM tableTransaction
WHERE debtToNum = My_Number
GROUP BY creditedToNum) Cr
WHERE db.transactionStatus = 1
AND db.isActive = 1;
It's returning 0 rows but the actual output should be as below
Here is my table structure:
| Amount | Credit_to | Debit_to |
|--------|-----------|-----------|
| 2000 | My_Number | Number_1 |
| 5000 | My_Number | Number_2 |
| 3000 | Number_1 | My_Number |
| 4000 | Number_2 | My_number |
| 2000 | My_Number | Number_2 |
What actual result I want is:
| Name | Amount |
|----------|--------|
| Number_1 | 1000 |
| Number_2 | 3000 |
There is no join condition in your query.
Instead of joining, it might be a better idea to use a compound query to bring the data into a useful form:
SELECT Credit_to AS Name, Amount
FROM tableTransaction
WHERE Debit_to = #My_Number
UNION ALL
SELECT Debit_to, -Amount
FROM tableTransaction
WHERE Credit_to = #My_Number;
| Name | Amount |
|----------|--------|
| Number_1 | 3000 |
| Number_2 | 4000 |
| Number_1 | -2000 |
| Number_2 | -5000 |
| Number_2 | -2000 |
Then just group it:
SELECT Name, SUM(Amount) AS Total
FROM (SELECT Credit_to AS other, Amount
FROM tableTransaction
WHERE Debit_to = #My_Number
UNION ALL
SELECT Debit_to AS other, -Amount
FROM tableTransaction
WHERE Credit_to = #My_Number)
GROUP BY Name
ORDER BY Name;
| Name | Total |
|----------|-------|
| Number_1 | 1000 |
| Number_2 | -3000 |

How to add values from different table that has the same id number?

I have 7 tables, each table has a column for total I need to get the total of each table which has the same id number, these are 3 sample tables:
clean table
clean_id | labor_expense | machine_expense | diesel_expense | clean_total
1 | 1000 | 2000 | 500 | 3500 2 | 2000 | 1000 | 1000 | 4500
plant_table
plant_id | labor_expense | machine_expense | plant_expense | plant_total
1 |
1000 |
2000 |
500 | 3500 2 | 2000 | 1000 | 1000 | 4500
fertilize_table
fertilize_id | labor_expense | machine_expense | fertilizer_expense | fertilize_total
1 |
1000 |
2000 |
500 | 3500 2 | 2000 | 1000 | 1000 | 4500
How can I get the total of clean_total, plant_total and fertilize_total that have the same id number?
I was thinking to save it to another table like total_expense but I only get the total of the first id number
total_expenses
total_id | clean_total | plant_total | fertilize_total | total_expenses
1 |
3500 |
3500 |
3500 | 10500 2 | 4500 | 4500 | 4500 | 13500
If the rows for each ID are known to exist in all tables, you can simply join them:
SELECT clean_id AS total_id,
clean_total + plant_total + fertilize_total AS total_expenses
FROM clean_table
JOIN plant_table ON clean_id = plant_id
JOIN fertilize_table ON clean_id = fertilize_id

Query based on the difference in number of occurrences in other tables

I have 3 tables.
table_main:
+-----+----------+-------------+
| _id | ENTRY_ID | DATA |
+-----+----------+-------------+
| 1 | a1 | some data 1 |
| 2 | a2 | some data 2 |
| 3 | a3 | some data 3 |
+-----+----------+-------------+
table_additions:
+----------+-----------+
| ENTRY_ID | TIMESTAMP |
+----------+-----------+
| a1 | 123456 |
| a2 | 123458 |
| a1 | 123654 |
| a1 | 123658 |
| a2 | 123843 |
| a3 | 123911 |
+----------+-----------+
table_deletions:
+----------+-----------+
| ENTRY_ID | TIMESTAMP |
+----------+-----------+
| a3 | 123556 |
| a2 | 123558 |
| a3 | 123754 |
| a1 | 123858 |
| a3 | 123863 |
| a3 | 123976 |
+----------+-----------+
I am working in android. I want to get the data from table_main of all the entries which have :
number of occurances in table_additions > number of occurances in
table_deletions
So, in the above example, the output should be:
+-----+----------+-------------+
| _id | ENTRY_ID | DATA |
+-----+----------+-------------+
| 1 | a1 | some data 1 |
| 2 | a2 | some data 2 |
+-----+----------+-------------+
This can be done with correlated subqueries:
SELECT *
FROM table_main
WHERE (SELECT COUNT(*)
FROM table_additions
WHERE entry_id = table_main.entry_id
) >
(SELECT COUNT(*)
FROM table_deletions
WHERE entry_id = table_main.entry_id
)

Categories

Resources