MySQL WHERE IN and another variable equal [closed] - mysql

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm running the following query expecting to receive back the first 3 rows, however all I am getting is the first row.
Query Ran
SELECT * FROM inventories WHERE id IN ('1,2,3') AND state = 1
Database
Result
[ RowDataPacket {
id: 1,
uid: 1,
name: 'An Epic Item',
suggested_price: 29212,
image: 'http://hanatemplate.com/images/emoji-ok-7.png',
state: 1 } ]
Why is it not returning the first three rows instead of just the first one?

It shouldn't return any, since you've got the list enclosed in quotes (but see below). Try
SELECT * FROM inventories WHERE id IN (1,2,3) AND state = 1
As per the comments, SQL converts your string to an integer, 1, which is why you get the first record.

Related

Calculating time difference returns null [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am trying to select everything from the post and calculate the time difference between you and when it was posted and I am using
SELECT FLOOR(TIME_TO_SEC(TIMEDIFF(CURRENT_TIMESTAMP, post.created) / 60))
FROM post
as my query to calculate it to minutes but when I execute this query some of the records return null and I do not know why this is
2021-12-16 21:31:09 the query does not work for this date
2021-12-18 17:01:37 but for this one it does
does anyone know a fix?
Based on this fiddle, it looks like it's failing at the TIME_TO_SEC() call:
SELECT FLOOR(TIME_TO_SEC(TIMEDIFF(CURRENT_TIMESTAMP, post.created) / 60)) as result,
timediff(current_timestamp, post.created) as step1,
TIMEDIFF(CURRENT_TIMESTAMP, post.created) / 60 as step2,
TIME_TO_SEC(TIMEDIFF(CURRENT_TIMESTAMP, post.created) / 60) as step3
FROM post
Results:
result
step1
step2
step3
null
45:09:30
7515.5000
null
151
01:39:02
231.7000
151
null
-46:20:58
-7700.9667
null
null
838:59:59
139765.9833
null
This is the kind of debugging step you ought to be able to take on your own :/
However, it is reasonable to still need help after narrowing the problem as above, and with that in mind this is probably the result of a mis-placed parentheses. It seems like you're want to divide by 60 after the TIME_TO_SEC() call:
FLOOR(TIME_TO_SEC(TIMEDIFF(CURRENT_TIMESTAMP, post.created)) / 60)

Open Json a Json field in SQL Server to table [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have a table in SQL Server where a row contains a Json column - something like this:
ResponseText
RequestId
{"LosUnqCod":0,"LosMidId":23}
96173722
{"LosUnqCod":1,"LosMidId":5}
96173721
I want to have a table in this shape:
LosUnqCod
LosMidId
RequestId
0
23
96173722
1
5
96173721
how to open this json?
Hi every body I found my answer and want to share with anyone who has the same issue as I does
SELECT
reqTbl.LosUnqCod ,
a.RequestId
FROM [dbo].[a] CROSS APPLY
OPENJSON( dbo.a.responsetext)
WITH (
LosUnqCod nvarchar(50)
) AS reqTbl
You don't have to use OPENJSON if you only have one JSON object (not an array) per SQL row, you can use JSON_VALUE instead:
SELECT
JSON_VALUE(a.responsetext, '$.LosUnqCod') LosUnqCod,
JSON_VALUE(a.responsetext, '$.LosMidId') LosMidId,
a.RequestId
FROM [dbo].[a];

Is there any sql-query to check the value is exist or not in db table [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In my database table have this value,i have to check this email value 'abc#gmail.com' is exist in the database or not.
abc#gmail.com
edf#dfg.com
xyz#abcinfo.com
12345#fdfg.com
If 'abc#gmail.com' value is exist in the database table, output is true else false, so how to write the query for this to achieve?
thanks
Kumar
You can do something as simple as
select CASE WHEN count(1) > 0 THEN 'true' ELSE 'false' END from table where email = 'abc#gmail.com'
But you should give some more information for a beater answer.
you can write query like
SELECT IF(COUNT(*) >0, TRUE,FALSE)as response FROM <tablename> WHERE emailid='edf#dfg.com';
select exists(
SELECT 1 FROM <tableName> WHERE email LIKE 'abc#gmail.com'
)

How to select not-wellformed data [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a huge table I'm trying to port to a new database, there's a row that stores number of doors, body style, and gearbox like "4SDa" for 4 door sedan, auto or "5HBm" for 5 door hatchback, manual.
I need to select all rows that AREN'T like this so I can edit them as they are causing issues.
How can I select any row that isn't in the format
number-letter-letter-letter' (case insensitive)
select * from your_table
where your_column not regexp '^[0-9][a-zA-Z]{3,3}$'
Regex Tester
SELECT '4SDa' REGEXP '^[0-9][a-z]{3}$'; --> 1
SELECT '4SDaa' REGEXP '^[0-9][a-z]{3}$'; --> 0
SELECT 'AAbb' REGEXP '^[0-9][a-z]{3}$'; --> 0

How to fetch mysql data having brackets? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to fetch mysql data having round brackets e.g. ABCD(XYZ). When I run query
"SELECT * FROM tablename WHERE Column = 'ABCD(XYZ)'"
it returns an empty result. Please suggest a way. Thanks in advance!
this should work:
INSERT INTO `tablename` (`Column`) VALUES ('ABCD(XYZ)');
SELECT * FROM `tablename` WHERE `Column` = 'ABCD(XYZ)'";
Maybe 'ABCD(XYZ)' is not exactly the value of your data (for example if you inserted some whitespaces before or after it.)
You can try it with a like to find that out:
SELECT * FROM `tablename` WHERE `Column` LIKE '%ABCD(XYZ)%'";
Another possibility is that your value has been converted with htmlentities and you saved something like this:
'ABCD&40;XYZ&41;'
&40; Left parenthesis
&41; Right parenthesis