My join sql query won't bring results - mysql

What could be wrong with my sql query here , I'd like to retrieve data from both tables meeting a WHERE condition
SELECT *, UNIX_TIMESTAMP(i.sent_date) AS udate
FROM ibc_sent_history as i INNER JOIN
ibc_messages as u
ON i.msg_ids = u.id
WHERE (i.sent_date >= '02-02-2013' AND i.sent_date <= '02-02-2014')
ORDER BY i.sent_date
LIMIT 200

Assuming your ibc_sent_history.sent_date datatype is DATETIME, here's a way to refactor this query. (This will work even if the datatype is DATE). You need to change your date input string format from 02-02-2013 to the more standard '2014-02-02` (YYYY-MM-DD).
SELECT whatever, whatever
FROM ibc_sent_history AS i
INNER JOIN ibc_messages AS u ON i.msg_ids = u.id
WHERE i.sent_date >= '2013-02-02'
AND i.sent_date < '2014-02-02' + INTERVAL 1 DAY
ORDER BY i.sent_date DESC
LIMIT 200
I changed the ORDER BY to include DESC. This is to return the most recent items, not the oldest. If that's not what you need, take off the DESC.
I changed the date formatting.
I changed the end of your selection range to
i.sent_date < '2014-02-02` + INTERVAL 1 DAY
That's because
i.sent_date <= '2014-02-02`
will include items that occur precisely at midnight on 2-Feb-2014, but won't include any other items on that day. What you probably want are items that occurred up to but NOT including midnight on the next day.

I don't know MySQL very well, but in SQL Fiddle when I run:
CAST('2014-02-02' AS DATE)
I get a date, when I run
CAST('02-02-2014' AS DATE)
I get NULL, so seems like your date format is wrong.
Demo: SQL Fiddle

Related

Why this WHERE condition that have to select records in a specified timeframe is not working as expected?

I am not so into database and I have the following problem working on this MySql query:
SELECT
CCMD.id AS crop_calendar_message_details_id,
CCMD.broadcasting_start_date AS broadcasting_start_date,
CCMD.broadcasting_end_date AS broadcasting_end_date,
CCMD.creation_date AS creation_date,
CCM.id AS message_id,
CCM.content_en AS content_en,
IFNULL(CCMN.content, CCM.content_en) AS content,
CCMN.audio_link AS audio_link,
CCMD.crop_action_details_id AS crop_action_details_id
FROM CropCalendarMessageDetails AS CCMD
INNER JOIN CropCalendarMessage AS CCM
ON CCMD.crop_calendar_message_id = CCM.id
LEFT JOIN CropCalendarMessageName AS CCMN
ON CCMN.crop_calendar_message_id = CCM.id AND CCMN.language_id = :language_id
INNER JOIN CropActionDetails AS CAD
ON CCMD.crop_action_details_id = CAD.id
WHERE
CCMD.commodity_id = 10
AND
CCMD.country_id = 2
AND
CAD.id = :cad_id
AND
CCMD.broadcasting_start_date >= CURDATE()
AND
CURDATE() <= CCMD.broadcasting_end_date
ORDER BY CCMD.broadcasting_start_date
I have some records that have the following fixed values for thes date fields:
CCMD.broadcasting_start_date = 22/12/2018 23:59:00
CCMD.broadcasting_end_date = 30/05/2018
So in theory my query should skip these values because I have set this section on my WHERE clause:
AND
CCMD.broadcasting_start_date >= CURDATE()
AND
CURDATE() <= CCMD.broadcasting_end_date
The problem is that these records are returned by my query so this dates filter condition is not working.
Why? What is wrong? What am I missing? How can I fix it?
When dealing with date/time values and querying, I personally have always tried to apply >= and < the boundaries. For example, if you wanted something for All activity within March, 2018, I would do
where '2018-03-01' <= DateTimeField
AND DateTimeField < '2018-04-01'
By doing greater or equal to the start of just a date, you get from midnight all the way through the date period. As for the ending date, I always go LESS than the following day (hence April 1st). So I get everything up to Mar 31 at 11:59:59pm.
This way you also dont need to mess with date conversion functions just to ensure something is on the same day or time-portions thereof.
Might this help in resolving the date/time considerations of your query.

check if a date exists within a grouped row and if it does then change column in sql

I have a database query that groups payments due into monthly values.
The problem I am facing now is that I can not see each individual payments to be able to make sure all are paid.
dp within the column is Date Paid.
If the date within the column is 0000-00-00 00:00:00 then no payment has been made.
Here is the query:
$monthlyQuery = $pdo->prepare("
SELECT SUM(net) AS net
, MONTH(dd) AS month
, YEAR(dd) AS year
, dp
FROM sin WHERE cpo=:cpo
AND dd >= :dd1
AND dd <= :dd2
GROUP
BY month
ORDER
BY dd ASC
");
$monthlyQuery->execute(array(':cpo' => $fetch['cpo'], ':dd1' => $janDate, ':dd2' => $decDate));
$monthlyQueryNum = $monthlyQuery->rowCount();
is there a way to check each dp column before the row is grouped and if at any point the date is 0000-00-00 00:00:00 then make all dates for that group 0000-00-00 00:00:00
I was thinking of doing a CASE WHEN THEN but the logic doesn't seem to work for me.
Thanks
As you haven't given us data to test this against, I've written the following without being able to test it. It should be close to what you want:
SELECT
SUM(s.net) AS net
,MONTH(s.dd) AS month
,YEAR(s.dd) AS year
,COALESCE(missing.inner_dp,s.dp) AS dp
FROM
sin s
LEFT JOIN
(
SELECT
YEAR(s2.dd) AS inner_year
,MONTH(s2.dd) AS inner_month
,cpo AS inner_cpo
,dp AS inner_dp
FROM
sin s2
WHERE
s2.dp = '0000-00-00 00:00:00'
AND s2.dd >= :dd1
AND s2.dd <= :dd2
GROUP BY
inner_year, inner_month, inner_cpo, inner_dp
) AS missing ON
YEAR(s.dd) = missing.inner_year
AND MONTH(s.dd) = missing.inner_month
AND s.cpo = missing.inner_cpo
WHERE
s.cpo=:cpo
AND s.dd >= :dd1
AND s.dd <= :dd2
GROUP BY
year, month, dp
ORDER BY
year, month, dp
I'm not a fan of MySQL allowing "implicit" GROUP BY expressions, and I've made them explicit. You'll notice I also changed the ORDER BY as the one you had looked problematic to me.

ORDER BY datetime depending on the day AND time

I have a database field of type datetime.
The name of this field is "recallDate"
I would like to order the results in the following way:
The results must be chronological in the time: from newest to oldest
The results must be grouped by date: in other words, result having the same date are together, grouped
For every day, the results must be chronological according to the hour: earliest to latest
The results having no hour ( 00:00:00 ) have to be at the end of the results of the day
This is my actual query :
SELECT a.recallDate, a.id, a.id_company, com.name, a.recallType
FROM PDT_CRM.actions a
INNER JOIN PDT_CRM.traders as trad on trad.id=a.id_traders
WHERE DATE(a.recallDate) > DATE(NOW() + INTERVAL 30 DAY)
ORDER BY TIME(a.recallDate) , a.recallType
It is very likely that I have to use CASE but I don't understand how to use it.
You can use the following code to create a specific order that will put times '00:00:00' at the very end of the day:
...
ORDER BY date(a.rappelDate),
case when time(a.rappelDate) = 0 then 1 else 0 end,
time(a.rappelDate)

SQL query to select data between dates does not show last date

im using a query to get data between dates but for some reason it does not pull the data of the last date selected here is my query:
SELECT * FROM order WHERE status = "completed" AND orderdate >= ? AND orderdate <= ? ORDER BY orderid DESC
Im using is equal to or less then... but still?
what am i doing wrong ?
SELECT * FROM order WHERE status = "completed" AND date(orderdate) >= date(?) AND date(orderdate) <= date(?) ORDER BY orderid DESC
It happened with me also, but in my case instead of passing a date I was querying using a datetime variable, Please make sure you are querying with date variable only.
Make sure that orderdate is date as well as your query parameter is also date, or use appropriate function to convert them in date, than query.
Your dates are actually datetimes - so you are actually, in the case of the upperbound, saying "12 midnight" on whichever date you choose. Hence, if it tries to test a value at say 10am in the morning, it fails as being outside the range.
Either set the upperbound date one day forward, or explicitly only test the date part of the datetime...

Mysql summary query with date range, multiple tables

Im running a sql query that is returning results between dates I have selected (2012-07-01 - 2012-08-01). I can tell from the values they are wrong though.
Im confused cause its not telling me I have a syntax error but the values returned are wrong.
The dates in my database are stored in the date column in the format YYYY-mm-dd.
SELECT `jockeys`.`JockeyInitials` AS `Initials`, `jockeys`.`JockeySurName` AS Lastname`,
COUNT(`runs`.`JockeysID`) AS 'Rides',
COUNT(CASE
WHEN `runs`.`Finish` = 1 THEN 1
ELSE NULL
END
) AS `Wins`,
SUM(`runs`.`StakeWon`) AS 'Winnings'
FROM runs
INNER JOIN jockeys ON runs.JockeysID = jockeys.JockeysID
INNER JOIN races ON runs.RacesID = races.RacesID
WHERE `races`.`RaceDate` >= STR_TO_DATE('2012,07,01', '%Y,%m,%d')
AND `races`.`RaceDate` <= STR_TO_DATE('2012,08,01', '%Y,%m,%d')
GROUP BY `jockeys`.`JockeySurName`
ORDER BY `Wins` DESC`
It's hard to guess what the problem is from your question.
Are you looking to summarize all the races in July and the races on the first of August? That's a slightly strange date range.
You should try the following kind of date-range selection if you want to be more precise. You MUST use it if your races.RaceDate column is a DATETIME expression.
WHERE `races`.`RaceDate` >= STR_TO_DATE('2012,07,01', '%Y,%m,%d')
AND `races`.`RaceDate` < STR_TO_DATE('2012,08,01', '%Y,%m,%d') + INTERVAL 1 DAY
This will pick up the July races and the races at any time on the first of August.
But, it's possible you're looking for just the July races. In that case you might try:
WHERE `races`.`RaceDate` >= STR_TO_DATE('2012,07,01', '%Y,%m,%d')
AND `races`.`RaceDate` < STR_TO_DATE('2012,07,01', '%Y,%m,%d') + INTERVAL 1 MONTH
That will pick up everything from midnight July 1, inclusive, to midnight August 1 exclusive.
Also, you're not using GROUP BY correctly. When you summarize, every column in your result set must either be a summary (SUM() or COUNT() or some other aggregate function) or mentioned in your GROUP BY clause. Some DBMSs enforce this. MySQL just rolls with it and gives strange results. Try this expression.
GROUP BY `jockeys`.`JockeyInitials`,`jockeys`.`JockeySurName`
My best guess is that the jocky surnames are not unique. Try changing the group by expression to:
group by `jockeys`.`JockeyInitials`, `jockeys`.`JockeySurName`
In general, it is bad practice to include columns in the SELECT clause of an aggregation query that are not included in the GROUP BY line. You can do this in MySQL (but not in other databases), because of a (mis)feature called Hidden Columns.