Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Post New Thread Reply

Register GFY Rules Calendar Mark Forums Read
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 08-14-2022, 08:23 AM   #1
Publisher Bucks
Confirmed User
 
Industry Role:
Join Date: Oct 2018
Location: New Orleans, Louisiana. / Newcastle, England.
Posts: 1,126
Date range SQL querie not working?

This is what I'm using which, from what I have seen online, should be working:

Quote:
"SELECT * FROM CookingShows WHERE Date BETWEEN '2022-01-09' AND Date '2022-01-10' ORDER BY Date DESC;");
Multiple sites are saying that is the correct format, yet it isnt displaying any data that I have in the table.

Any thoughts?
__________________
SOMETHING EXTREME IS COMING SOON!
Publisher Bucks is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-14-2022, 08:48 AM   #2
sarettah
see you later, I'm gone
 
Industry Role:
Join Date: Oct 2002
Posts: 14,072
Quote:
Originally Posted by Publisher Bucks View Post
This is what I'm using which, from what I have seen online, should be working:


"SELECT * FROM CookingShows WHERE Date BETWEEN '2022-01-09' AND Date '2022-01-10' ORDER BY Date DESC;");

Multiple sites are saying that is the correct format, yet it isnt displaying any data that I have in the table.

Any thoughts?
1. You have the syntax wrong. The correct syntax is "where date between date1 and date2" not "where date between date1 and date date2".

2. You are trying to compare a string to a date. You need to cast the strings as dates if you want a date compare. "where date between cast(date1 as date) and cast(date2 as date)"

3. Date is a keyword in MYSQL and should not be used as a variable name or field name. So where you have your column called date ("where Date...") should be something like "where order_date...." Or whatever other name the date is stored as.


.
__________________
All cookies cleared!
sarettah is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-14-2022, 09:09 AM   #3
Publisher Bucks
Confirmed User
 
Industry Role:
Join Date: Oct 2018
Location: New Orleans, Louisiana. / Newcastle, England.
Posts: 1,126
Quote:
Originally Posted by sarettah View Post
[b]2. You are trying to compare a string to a date. You need to cast the strings as dates if you want a date compare. "where date between cast(date1 as date) and cast(date2 as date)"]/b]

.
Thanks.

I renamed the column to showdate and also redid the syntax, which is strange as StackOverFlow had tons of people saying that worked as I posted lol

Can you explain that part above for me a little? I'm not quite sure what you mean.
__________________
SOMETHING EXTREME IS COMING SOON!
Publisher Bucks is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-14-2022, 09:54 AM   #4
redwhiteandblue
Bollocks
 
redwhiteandblue's Avatar
 
Industry Role:
Join Date: Jun 2007
Location: Bollocks
Posts: 2,792
MySQL (and PHP for that matter) does not magically recognize '2022-01-10' as a date. It's just a string with some numbers and hyphens in it. You can compare two strings but it's not the same as comparing dates. You have to tell it it's a date by casting it from "string" type to "date" type.
redwhiteandblue is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-14-2022, 10:04 AM   #5
Klen
 
Klen's Avatar
 
Industry Role:
Join Date: Aug 2006
Location: Little Vienna
Posts: 32,235
Sample of code which work for me:

PHP Code:
SELECT SUM(`P`) AS 'M' FROM `CWHERE `DateTimebetween '$yy-$mm-$dd 00:00:00' AND '$yy-$mm-$dd 23:59:59' 
Klen is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-14-2022, 10:16 AM   #6
blackmonsters
Making PHP work
 
blackmonsters's Avatar
 
Industry Role:
Join Date: Nov 2002
Location: 🌎🌅🌈🌇
Posts: 20,268
Edit : already posted above by Sarettah




__________________
Make Money with Porn
blackmonsters is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-14-2022, 11:10 AM   #7
Publisher Bucks
Confirmed User
 
Industry Role:
Join Date: Oct 2018
Location: New Orleans, Louisiana. / Newcastle, England.
Posts: 1,126
So this is what I have now:

Quote:
$result = mysqli_query($con,"SELECT * FROM CookingShows WHERE ShowDate (date as DATE) BETWEEN CAST('2022-09-01' AS DATE) AND CAST('2022-10-01' AS DATE)");
It still doesnt show me any data.

The column is set to varchar(255) if that makes any difference? It isnt set as date, datetime, or anything like that, should it be?
__________________
SOMETHING EXTREME IS COMING SOON!
Publisher Bucks is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-14-2022, 11:10 AM   #8
Publisher Bucks
Confirmed User
 
Industry Role:
Join Date: Oct 2018
Location: New Orleans, Louisiana. / Newcastle, England.
Posts: 1,126
Quote:
Originally Posted by Klen View Post
Sample of code which work for me:

PHP Code:
SELECT SUM(`P`) AS 'M' FROM `CWHERE `DateTimebetween '$yy-$mm-$dd 00:00:00' AND '$yy-$mm-$dd 23:59:59' 
Thanks will give that a try now
__________________
SOMETHING EXTREME IS COMING SOON!
Publisher Bucks is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-14-2022, 02:30 PM   #9
sarettah
see you later, I'm gone
 
Industry Role:
Join Date: Oct 2002
Posts: 14,072
Quote:
WHERE ShowDate (date as DATE)
Where did you get that from?

It should be "where ShowDate between......"

Unless you are storing a string in the database instead of a date. In that case it would be

"where cast(ShowDate as date) between....

.
__________________
All cookies cleared!
sarettah is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-14-2022, 05:04 PM   #10
Publisher Bucks
Confirmed User
 
Industry Role:
Join Date: Oct 2018
Location: New Orleans, Louisiana. / Newcastle, England.
Posts: 1,126
Quote:
Originally Posted by sarettah View Post
Where did you get that from?

It should be "where ShowDate between......"

Unless you are storing a string in the database instead of a date. In that case it would be

"where cast(ShowDate as date) between....

.
I threw it together lol

Thank you
__________________
SOMETHING EXTREME IS COMING SOON!
Publisher Bucks is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-15-2022, 12:42 AM   #11
k0nr4d
Confirmed User
 
k0nr4d's Avatar
 
Industry Role:
Join Date: Aug 2006
Location: Poland
Posts: 9,228
Quote:
Originally Posted by redwhiteandblue View Post
MySQL (and PHP for that matter) does not magically recognize '2022-01-10' as a date. It's just a string with some numbers and hyphens in it. You can compare two strings but it's not the same as comparing dates. You have to tell it it's a date by casting it from "string" type to "date" type.
Mysql actually does - you don't have to cast it as a date. BETWEEN '2022-01-01' AND '2022-12-31' will work fine. It's possible it might not work if you have strict mode on but overall it does parse it by itself.
k0nr4d is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-15-2022, 06:07 AM   #12
redwhiteandblue
Bollocks
 
redwhiteandblue's Avatar
 
Industry Role:
Join Date: Jun 2007
Location: Bollocks
Posts: 2,792
Quote:
Originally Posted by k0nr4d View Post
Mysql actually does - you don't have to cast it as a date. BETWEEN '2022-01-01' AND '2022-12-31' will work fine. It's possible it might not work if you have strict mode on but overall it does parse it by itself.
It will work but technically it's because the string comparison works with the date in that format. MySQL doesn't know those strings contain dates until you do something date related with them like assigning them to a date column.
redwhiteandblue is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-15-2022, 02:47 PM   #13
Nitzer Ebb
Confirmed User
 
Nitzer Ebb's Avatar
 
Industry Role:
Join Date: Apr 2015
Location: Stjørdal, Norge
Posts: 796
Quote:
Originally Posted by Publisher Bucks View Post
This is what I'm using which, from what I have seen online, should be working:



Multiple sites are saying that is the correct format, yet it isnt displaying any data that I have in the table.

Any thoughts?
remove date after AND
__________________
-= Krampus Productions =-
Nitzer Ebb is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-15-2022, 02:51 PM   #14
Publisher Bucks
Confirmed User
 
Industry Role:
Join Date: Oct 2018
Location: New Orleans, Louisiana. / Newcastle, England.
Posts: 1,126
Thanks everyone, I have this working now
__________________
SOMETHING EXTREME IS COMING SOON!
Publisher Bucks is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-16-2022, 12:44 AM   #15
k0nr4d
Confirmed User
 
k0nr4d's Avatar
 
Industry Role:
Join Date: Aug 2006
Location: Poland
Posts: 9,228
Quote:
Originally Posted by redwhiteandblue View Post
It will work but technically it's because the string comparison works with the date in that format. MySQL doesn't know those strings contain dates until you do something date related with them like assigning them to a date column.
Well I mean yeah, I suppose you are right in that respect - it works if you use a DATE or TIMESTAMP field but I suppose it might not work without casting if you have it in a VARCHAR or something.
k0nr4d is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks

Tags
date, table, desc;;, multiple, sites, format, displaying, data, correct, range, sql, querie, online, 2022-01-09, 2022-01-10, cookingshows, select
Thread Tools



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.