How to achieve mysql WHERE IN in CouchBase? [duplicate] - couchbase

The following query works just fine when only IN operator is used
SELECT META().id FROM bucket_name WHERE description IN ['Item1','Item2']
But when I fire this query it gives me a blank result
SELECT META().id FROM bucket_name WHERE id = 123 AND description IN ['Item1','Item2']
Am I doing something wrong or somebody else has faced the same problem?

I think you have to take your "IN" condition into parenthesis to make it work:
SELECT META().id FROM bucket_name WHERE id = 123 AND (description IN ['Item1','Item2'])
It has to do with the precedence level of the operators evaluation by N1QL processor
If you run it with EXPLAIN keyword it will show how it links conditions against each other.
e.g.
explain SELECT META().id FROM bucket_name WHERE id = 123 AND (description IN ['Item1','Item2'])
vs
explain SELECT META().id FROM bucket_name WHERE id = 123 AND description IN ['Item1','Item2']

With the latest N1QL developer preview (http://docs.couchbase.com/developer/n1ql-dp3/n1ql-intro.html) the IN clause does not need to be parenthesized, so this should work:
SELECT META(b).id FROM bucket_name b WHERE id = 123 AND description IN ['Item1','Item2']
You need to pass the bucket name (or alias) to META() I think because N1QL now supports queries on multiple buckets.

Related

Difference between GROUP BY HAVING and SELECT MAX() GROUP BY

I'm new to SQL and getting confused by the difference in the following two queries:
SELECT MAX(version), * FROM table WHERE primary_key = #key GROUP BY location
SELECT version, * FROM table WHERE primary_key = #key GROUP BY location HAVING version = MAX(version)
Assuming that the table looks like something like this:
primary_key | version | location | data
If I'm understanding this correctly, both queries select the max version entry within each location (among those that have #key as primary key). So is there any difference between the two queries? Or is the difference just on performance?
You missed the table name
the use of * star (all columns) when you are using aggregation function is deprecated and in most db is not allowed
SELECT MAX(version)
FROM your_table
WHERE primary_key = #key
GROUP BY location
SELECT version
FROM your_table
WHERE primary_key = #key
GROUP BY location
HAVING version = MAX(version)
the two qyery are different because in the second the resulting query is filter for match the having condition
having work ever on the result of a query (instead where work directly on the row source for the query )
and yes the secondo is more slow that the first ... is most case the difference could be not appreciable
Please refer below link, may be useful:
http://www.dofactory.com/sql/group-by

Multiple like condition in mysql

When i use multiple like condition with AND keyword, it will return false value actually, it had value in table and my query as below:
SELECT * FROM tbl_student
WHERE name LIKE '%jame%'
AND ( id LIKE '%001%'
AND id2 LIKE '%002%'
)
This query return empty row
SELECT * FROM tbl_student
WHERE name LIKE '%jame%'
OR ( id LIKE '%001%'
AND id2 LIKE '%002%'
)
This query return data
tbl_student
Name ID ID2
jame 233 3234
jame333 1001 0222
jame333 Da 1001 1002
Why it is returned empty data when use AND keyword ?
Your query with the AND operator does not return the desired data, because your conditions placed within brackets always evaluate to false.
The reason this occurs is because you are trying to use LIKE '%str%' on an integer. This will not work expectedly. If you insist on using LIKE, you might want to CAST your integers to strings:
SELECT *
FROM tbl_student
WHERE name LIKE '%jame%'
AND (CAST(id AS CHAR) LIKE '%001%' AND
CAST(id2 AS CHAR) LIKE '%002%');
FYI, an e1 AND (e2 AND e3) logical expression remains the same with the brackets omitted.

Query in mysql Stored Proc falsely returning null

I am attempting to set a list of variables using a SELECT - INTO statement, however I am hitting some issues with the query returning null data when it should find a row. The really odd part is, if I select a certain column it works, but it won't work with any of the others. To isolate the issue one at a time, I have simplified the query. Here is what I am trying to run:
SELECT Product_1 INTO #Product_1
FROM STG_XREF_ELS_PRODUCTS
WHERE MarketName = #ThisMarketName
LIMIT 1;
This statement does not work, however if I enter the same exact query but select a different field, it works fine...
SELECT MarketName INTO #Product_1
FROM STG_XREF_ELS_PRODUCTS
WHERE MarketName = #ThisMarketName
LIMIT 1;
Notably, I have tested to make sure that this is not a case where Product_1 actually is null, there is no instance in my xref table in which Product_1 is null. Also, if I just select Product_1 from the table with a limit of 1 and no where clause, I still get a null result.
I have also attempted using:
set #Product1 = (select Product_1
FROM STG_XREF_ELS_PRODUCTS
WHERE MarketName = #ThisMarketName);
Thanks in advance for your help!
P.S.
Try to change the name of the variable so:
SELECT Product_1 INTO #Product_variable
FROM STG_XREF_ELS_PRODUCTS
WHERE MarketName = #ThisMarketName
LIMIT 1;

MySQL request from two tables interacting with each other

Tables i have
Table1 user_id, date
Table2 user_id, status
I need something like
UPDATE Table2
SET status = 2
WHERE user_id in ( {SELECT user_id FROM Table1 WHERE date > 0} )
In other words i need to see if date in table1 is more than 0000-00-00 then grab user_id of people who match this criteria and use them in table2 to set their status to 2.
Problem is that i need to do it for more than one user so request inside request does not work for me, it only works when there's one row in result.
I think what you've written should work, but what's with the curly braces? Just remove them to make it like this:
UPDATE Table2
SET status = 2
WHERE user_id in (SELECT user_id FROM Table1 WHERE date > 0)
According to the MySQL documentation, IN should work with a subquery.
The ANY keyword, which must follow a comparison operator, means “return TRUE if the comparison is TRUE for ANY of the values in the column that the subquery returns.”
When used with a subquery, the word IN is an alias for = ANY.
Are you using PHP or some other language to make SQL calls? If so, it might easier to just,
firstly: select user ids from table 1
secondly: loop through each row
thirdly: execute the update
Regards,
Mark

MySQL select with subquery having replace

So I have a data with format like ;1;;2; and then I need to use this number in a query so I thought I'd convert it to 1,2 and use that in a IN condition. In my table, the result should return 2 rows but instead it is returning only 1 row.
My query is like this. The subquery return 1,2 with no problem but only 1 row is retrieve.
select *
from wt_lists
where id IN ((select replace (replace(sendto, ';;',','),';','')
from wt_stats where statsid IN (1)))
But when I try it with this. It returns the correct result, which in my case is 2 rows.
select *
from wt_lists
where id IN (1,2)
What am I missing here?
Comma delimited strings need to be explicitly defined in the query in order to be used in the IN clause - there's countless examples on SO where people need to use dynamic SQL to incorporate user submitted comma delimited strings.
That said, I have a solution using the FIND_IN_SET function:
SELECT DISTINCT wl.*
FROM WT_LISTS wl
JOIN (SELECT REPLACE(REPLACE(ws.sendto, ';;',','),';','') AS ids
FROM WT_STATS ws
WHERE ws.statsid = 1) x ON FIND_IN_SET(wl.id, x.ids) > 0
You are replacing the string:
';1;;2;'
To:
'1,2'
So, you SQL query looks like:
select * from wt_lists where id IN ('1,2') from wt_stats where statsid IN (1)
To use IN clause you need select different values in different rows.
I found this store procedure that does exactly what you need.
http://kedar.nitty-witty.com/blog/mysql-stored-procedure-split-delimited-string-into-rows/
I have not tested, but it is the way.
Obs: Like David said in the comments above, parsing the data in your application is a better way to do this.