I am trying to build sqlite view vwCOMPLETE_INFO from 2 tables (CUSTOMERS and CITIES)
but i have problem in merging the FIRST_NAME and LAST_NAME to build FULL_NAME Column in the new view
CREATE VIEW vwCOMPLETE_INFO AS SELECT (CUSTOMERS.FIRST_NAME || CUSTOMERS.LAST_NAME AS FULL_NAME), CUSTOMERS.AGE, CITIES.NAME FROM CITIES JOIN CUSTOMERS ON CUSTOMER.CITY_ID = CITY.ID;
what could fix that ?
The issue is that you are including AS FULL_NAME as part of the term that builds the column's data, rather than where it should be as after the term so that it names the column accordingly.
In short AS FULL_NAME should be the other-side of/outside/after the parenthesis's. i.e. ....CUSTOMERS.LAST_NAME) AS FULL_NAME..... rather than .....CUSTOMERS.LAST_NAME AS FULL_NAME).....
Try
CREATE VIEW vwCOMPLETE_INFO AS SELECT (CUSTOMERS.FIRST_NAME || CUSTOMERS.LAST_NAME) AS FULL_NAME, CUSTOMERS.AGE, CITIES.NAME FROM CITIES JOIN CUSTOMERS ON CUSTOMER.CITY_ID = CITY.ID;
Related
I have a rather big query that is returning data when executed outside android while returning nothing when executed within android.
I split the query in several pieces and determined that the union was ok.
I tried on a smaller set of data with the same behavior.
I've tested with different hardware and API versions.
I'm using the rawQuery method with constant values.
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#rawQuery(java.lang.String, java.lang.String[])
This query was meant to replace a FULL OUTER JOIN which is not currently supported.
SELECT IFNULL(stype, gtype) AS type, IFNULL(sdate, gdate) AS date, IFNULL(sroute, groute) AS route FROM (
SELECT sp.type AS stype, sp.date AS sdate, -1 AS gtype, gp.date AS gdate, sp.route AS sroute, gp.route AS groute
FROM Sensor_Point AS sp LEFT JOIN GPS_Point AS gp ON gp._id IS NULL AND sp.sent=0 AND sp.route=gp.route AND sp.route=1
UNION ALL
SELECT sp.type AS stype, sp.date AS sdate, -1 AS gtype, gp.date AS gdate, sp.route AS sroute, gp.route AS groute
FROM GPS_Point AS gp LEFT JOIN Sensor_Point AS sp ON sp._id IS NULL AND gp.sent=0 AND sp.route=gp.route AND gp.route=1
) WHERE route=1 ORDER BY date ASC LIMIT 255
Any hints would be greatly appreciated.
Update:
Look's like the problem is finally with the query parameters, if I set it this way:
String[] args = new String[3];
args[0] = args[1] = args[2] = "1";
Cursor data dataBase.rawQuery(SELECT_POINTS, args);
It doesn't work, while it works when hardcoding values directly in the query.
Cursor data = dataBase.rawQuery(SELECT_POINTS, null);
In the Android database API, all query parameters are strings.
(This is a horrible design mistake.)
Your query corresponds to:
... AND sp.route='1'
Try to convert the parameter strings back into a number like this:
... AND sp.route = CAST(? AS INT)
or just put the number directly into the query string.
I want to select recipe_id for recipes that have my both ingredient but I cant use AND like this(I mean it returns null for all columns) :
SELECT
vsearch.ingredient_id,
vsearch.ingredtypes_id,
vsearch.name,
vsearch.recipe_id,
vsearch.qingred
FROM
vsearch
WHERE
vsearch.name = 'olive' AND vsearch.name = 'apple'
vsearch is a View which is:
SELECT
ingredient.ingredient_id,
ingredient.ingredtypes_id,
ingredient.name,
quantity.recipe_id,
quantity.ingredient_id AS qingred
FROM
ingredient ,
quantity
WHERE
strong textingredient.ingredient_id = quantity.ingredient_id
And heres my database tables:
To find (for example) find the recipe id's that contain both cucumber and tomato;
SELECT recipe_id
FROM vsearch
WHERE name = 'cucumber' OR name = 'tomato'
GROUP BY recipe_id
HAVING COUNT(*) = 2
...where 2 is the name of ingredients that have to match (in this case both)
An SQLfiddle to test with.
select *
from vsearch
where recipe_id
in
( select recipe_id from vsearch where name = 'apple'
intersect
select recipe_id from vsearch where name = 'olive'
)
I have a rather big query that is returning data when executed outside android while returning nothing when executed within android.
I split the query in several pieces and determined that the union was ok.
I tried on a smaller set of data with the same behavior.
I've tested with different hardware and API versions.
I'm using the rawQuery method with constant values.
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#rawQuery(java.lang.String, java.lang.String[])
This query was meant to replace a FULL OUTER JOIN which is not currently supported.
SELECT IFNULL(stype, gtype) AS type, IFNULL(sdate, gdate) AS date, IFNULL(sroute, groute) AS route FROM (
SELECT sp.type AS stype, sp.date AS sdate, -1 AS gtype, gp.date AS gdate, sp.route AS sroute, gp.route AS groute
FROM Sensor_Point AS sp LEFT JOIN GPS_Point AS gp ON gp._id IS NULL AND sp.sent=0 AND sp.route=gp.route AND sp.route=1
UNION ALL
SELECT sp.type AS stype, sp.date AS sdate, -1 AS gtype, gp.date AS gdate, sp.route AS sroute, gp.route AS groute
FROM GPS_Point AS gp LEFT JOIN Sensor_Point AS sp ON sp._id IS NULL AND gp.sent=0 AND sp.route=gp.route AND gp.route=1
) WHERE route=1 ORDER BY date ASC LIMIT 255
Any hints would be greatly appreciated.
Update:
Look's like the problem is finally with the query parameters, if I set it this way:
String[] args = new String[3];
args[0] = args[1] = args[2] = "1";
Cursor data dataBase.rawQuery(SELECT_POINTS, args);
It doesn't work, while it works when hardcoding values directly in the query.
Cursor data = dataBase.rawQuery(SELECT_POINTS, null);
In the Android database API, all query parameters are strings.
(This is a horrible design mistake.)
Your query corresponds to:
... AND sp.route='1'
Try to convert the parameter strings back into a number like this:
... AND sp.route = CAST(? AS INT)
or just put the number directly into the query string.
I have db with scheme
1. _id
2. word
And i have ArrayList with for example 5 words (a1, a2, a3, a4, a5)
What is the best way to construct query to get words from DB which don't contain words from ArrayList?
Sowthing like
Select * from MYTABLE where WORD not in "all words from
ArrayList"
Build a string representing the set of words, and use that as an argument to the query.
StringBuilder wordSet = new StringBuilder();
wordSet.append('(');
for( String word : wordsList )
{
if(wordSet.length() > 1)
wordSet.append(',');
wordSet.append(word);
}
wordSet.append(')');
Pseudo Query
Select * from mytable
Use the except operator against a selection containing words in array list
http://en.wikipedia.org/wiki/Set_operations_(SQL)#EXCEPT_operator
I need to use classes to represent entities in database, here are some information:
=== TABLEs ===
SHOP
shop_id(primary)
name
owner
FAVOURITES LIST
fav_id(primary)
list_name
(JOIN)FAV_SHOPS
fav_id(primary)
shop_id(primary)
If I use a class Shop to represent shop entity, and FavShops to represent fav_shops, FavShops is written as below:
class FavShop {
int fav_id;
String list_name;
NSSet<Shop> shops;
}
Then how do I retrieve fav_shops from database in SQLite(Android)? I'll be appreciated if any one can provide some SQL statement, I also need to save the list to database, say, I added another shop to FavShop "My Favourites", how do I save it back to database?
Thanks!
You can use JOIN to get your FavShops table..
SELECT * FROM fav_shops INNER JOIN favourites_list ON fav_shops.fav_id = favourite_list.fav_id WHERE favourite_list.fav_id = <YOUR_LIST_ID>
You can put whatever clause you need in your WHERE (shop_id = ?, list_name = ?, etc..)
And to insert a new row in the fav_shops table
INSERT INTO fav_sops (fav_id, shop_id) values ('My Favourites', <A_SHOP_ID>);