Calculating time difference returns null [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 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)

Related

MySQL WHERE IN and another variable equal [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 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.

Random Number in MySQL column [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to add random number in one of my database column called likes which have about 5000 row in it. currently I have tried like below to get random number from 10 to 50.
$randomnumberlike = (rand(10,50));
but this is setting same number in all row...instead I want different number in row...anyone can please suggest how can I do it ?
Thanks
Try: UPDATE Table SET fieldName = ((rand() * 1000) % 40) + 10;
Because when you preset it in a variable, it'll be the same for all...
You're setting a variable:
$randomnumberlike = (rand(10,50));
If you're then using $randomnumberlike to add to the db then it will always be the same, its already be assigned (unless within a loop), you need to use (rand(10,50)) (or working similar) directly in the query like this:
UPDATE <table_name> SET <field_name> = ROUND((RAND() * (50-10))+10) WHERE ...;

query to retrieve correct properties in the range [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to retrieve all the properties in the range 2200000(min_price) to 2700000(max_price)
The query should look like
SELECT *
FROM properties
WHERE ( ((raw_min >= '{$min_price}')
OR (raw_min <= '{$max_price}'))
AND ((raw_max >= '{$min_price}')
OR (raw_max <= '{$max_price}')))
AND (..)
query should satisfy the below 6 conditions
raw_min - raw_max
(1) 1000000-2000000 (false)
(2) 1500000- 2400000 (true)
(3) 2300000-2600000 (true)
(4) 2500000-3000000 (true)
(5) 3200000-5000000 (false)
(6) 2000000-3000000 (true)
Please try this query:-
SELECT *
FROM properties
WHERE (raw_min between 2200000 AND 2700000)
OR (raw_max between between 2200000 AND 2700000)
OR (raw_min <= 2200000 && raw_max >= 2700000)
Please check I have updated the query.

Date is showed with unexpected value in html [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I used Bootstrap datepicker to get date from the user. I stored that value in database.
But when i render this date in html, it showed unexpected value with the date !
I give the screen shot of the result.
Can anyone help me to solve this ?
How have you stored the date in the database?
Make sure you used DATETIME and not VARCHAR
Also how did you get the date from the database?
I would use:
$query = "SELECT DATE_FORMAT(ColumnName, '%Y-%m-%d %H:%i:%s') AS Date FROM Table";
$result = mysqli_query($cxn,$query) or die(mysqli_error());
$date = $result->fetch_object()->Date;

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