I am writing the below specified query which has no error but doesn't fetch me the correct record which matches current date of server, on other part it shows me old records
My Mysql Query:
select news_title,
full_url,
source_site,
date_added,
`category`
from tbl_urlLog
where category like '%enter%'
or category like '%hollywood%'
or category like '%bollywood%'
or category like '%movies%'
or category like '%film%'
and date (`date_added`) = date (CURDATE())
group by `source_site`
order by `date_added` desc LIMIT 3
I am guessing you want to group together all the ORs using parentheses. Also, curdate() already is date no need to call date() on it.
select news_title,
full_url,
source_site,
date_added,
`category`
from tbl_urlLog
where (
category like '%enter%'
or category like '%hollywood%'
or category like '%bollywood%'
or category like '%movies%'
or category like '%film%'
)
and date (`date_added`) = CURDATE()
group by `source_site`
order by `date_added` desc LIMIT 3
Related
I am trying to pickup Account with End Date NULL first then latest date if there are more accounts with the same item
Table Sample
Result expected
Select distinct *
from Sample
where End Date is null
Need help to display the output.
Select *
from Sample
order by End_Date is not null, End_date desc
According to sample it seems to me you need union and not exists corelate subquery
select * from table_name t where t.enddate is null
union
select * from table_name t
where t.endate=( select max(enddate) from table_name t1 where t1.Item=t.Item and t1.Account=t.Account)
and not exists ( select 1 from table_name t2 where enddate is null and
t1 where t2.item=t.item
)
SELECT * FROM YourTable ORDER BY End_Date IS NOT NULL, End_Date DESC
In a Derived Table, you can determine the end_date_to_consider for every Item (using GROUP BY Item). IF() the MIN() date is NULL, then we consider NULL, else we consider the MAX() date.
Now, we can join this back to the main table on Item and the end_date to get the required rows.
Try:
SELECT t.*
FROM
Sample AS t
JOIN
(
SELECT
Item,
IF(MIN(end_date) IS NULL,
NULL,
MAX(end_date)) AS end_date_to_consider
FROM Sample
GROUP BY Item
) AS dt
ON dt.Item = t.Item AND
(dt.end_date_to_consider = t.end_date OR
(dt.end_date_to_consider IS NULL AND
t.end_date IS NULL)
)
First of all you should state clearly which result rows you want: You want one result row per Item and TOU. For each Item/TOU pair you want the row with highest date, with null having precedence (i.e. being considered the highest possible date).
Is this correct? Does that work with your real accounts? In your example it is always that all rows for one account have a higher date than all other account rows. If that is not the case with your real accounts, you need something more sophisticated than the following solution.
The highest date you can store in MySQL is 9999-12-31. Use this to treat the null dates as desired. Then it's just two steps:
Get the highest date per item and tou.
Get the row for these item, tou and date.
The query:
select * from
sample
where (item, tou, coalesce(enddate, date '9999-12-31') in
(
select item, tou, max(coalesce(enddate, date '9999-12-31'))
from sample
group by item, tou
)
order by item, tou;
(If it is possible for your enddate to have the value 9999-12-31 and you want null have precedence over this, then you must consider this in the query, i.e. you can no longer simply use this date in case of null, and the query will get more complicated.)
I'm grouping results by SERIAL_NUMBER and I'd like to display the last records for each group according to record ID DESC this what I've got so far:
SELECT * FROM
(SELECT `SERIAL_NUMBER`, `PART_NUMBER` , `POSITION` , `DUE_CAP_CHECK_DATE` , `DUE_OVERHAUL_DATE`
FROM `history_card` ORDER BY `HISTORY_ID` DESC ) AS X
GROUP BY `SERIAL_NUMBER`
But It does not return the last record, it returns the first one ;(
You are misusing the heinously confusing nonstandard MySQL extension to GROUP BY. Read this. https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html (This extension is like a talking horse. We don't wonder that it works badly. We wonder that it works at all.)
You can get the latest (largest) value of HISTORY_ID for each value of SERIAL_NUMBER from your table like this:
SELECT MAX(HISTORY_ID) FROM history_card GROUP BY SERIAL_NUMBER
Then you can use that set of HISTORY_ID values to retrieve what you want from your table.
SELECT SERIAL_NUMBER, PART_NUMBER, POSITION, DUE_CAP_CHECK_DATE, DUE_OVERHAUL_DATE
FROM history_card
WHERE HISTORY_ID
IN (SELECT MAX(HISTORY_ID) FROM history_card GROUP BY SERIAL_NUMBER)
I have a simple table example:
table name SHOW with 3 column ( id, title, representationDate).
I want to select all show and place it in order of date with distinct title because I can have the same title but at different representationDate. I want also that when I do my select query that any expire show be in the bottom of my list and all none expire show at the top by order of representationDate.
Right now I try this but don't give me the result I want.
SELECT distinct title as title
FROM SHOW
WHERE id = 23
AND representationDate > NOW()
UNION
(SELECT distinct title as title
FROM SHOW
WHERE id = 23
AND representationDate < NOW()
ORDER BY representationDate ASC)
The problem you obviously have encountered it that with unions you can order any particular parenthesized SELECT statement, but when performing the UNION across the results of the individual SELECT's, order is not guaranteed, so you could have interleaved results. You can order the overall UNIONed result set (outside of parenthesis at the end), but this will not get you what you are looking for as this would not allow you to differentiate expired records from non-expired records.
What you might want to do is to generate a calculated field by which you can sort:
SELECT
distinct title,
(CASE when representationDate >= NOW() THEN 1 ELSE 0 END CASE) as `current`
FROM SHOW
WHERE id = 23
ORDER BY `current` DESC, representationDate ASC
In a table that has the columns: 'product_id', 'price', ... (and others). I need to obtain the record with the lowest and unique price for a given product_id.
I tried different sentences without having the expected result. Finally I came across this sentence (for product_id = 2):
SELECT `price` , `product_id`
FROM bids
WHERE `product_id` = 2
GROUP BY `price`
HAVING COUNT( `price` ) = 1
ORDER BY `price`
LIMIT 1;
But it seems not to be an elegant solution having recourse to LIMIT and ORDER. How can I obtain the same record using the MIN() function or even something else?
This should work because you are already specifying the product_id to analyze:
SELECT MIN(t1.price) AS price, t1.product_id
FROM
(
SELECT price, product_id
FROM bids
WHERE product_id = 1
GROUP BY price, product_id
HAVING COUNT(price) = 1
) t1
Notes: MIN/MAX vs ORDER BY and LIMIT
Skydreamer, I'm not sure but as I understand it the op wants the the first unique value.
If the prices are 1,1,2,2,3 the query should return the row with the price of 3.
Ok, need a MySQL guru here. I am trying to write a query that will serve as a notification system for when someone leaves a comment on an item that you have previously commented on. The 'drinkComment' table is very simple:
commentID, userID, drinkID, datetime, comment
I've written a query that will get all of the comments on drinks that I have previously commented on (that are not mine), but it will still show comments that occurred BEFORE my comment. This is as close to what I would think would work, but it does not. Please help!
select #drinkID:=drinkComments.drinkID, commentID, drinkID, userID, comment, datetime
FROM drinkComments
WHERE `drinkID` IN
( select distinct drinkID from drinkComments where drinkComments.userID = 1)
AND drinkComments.dateTime > (
/*This gets the last date user commented on the main query's drinkID*/
select datetime FROM drinkComments WHERE drinkComments.userID = 1 AND drinkComments.drinkID = #drinkID ORDER BY datetime DESC LIMIT 1
)
ORDER BY datetime DESC
Why not start with a prequery of the user and all the drinks they've offered comments and as of what time (don't know if you have multiple comments per person for any given drink or not). Then, find comments from all others AFTER such of your date/time comment...
This query should actually be faster as it is STARTING with only ONE USER's drink comments as a basis, THEN goes back to the comments table for those matching the drink ID and cutoff time.
SELECT STRAIGHT_JOIN
dc.*
from
( select
drinkID,
max( datetime ) UserID_DrinkCommentTime
FROM
drinkComments
WHERE
userID = 1
group by
drinkID ) PreQuery
join DrinkComments dc
on PreQuery.DrinkID = dc.DrinkID
and dc.datetime > PreQuery.UserID_DrinkCommentTime
order by
dc.DateTime desc
I think you need to relate your innermost query to the middle query by drinkID.
select #drinkID:=drinkComments.drinkID, commentID, drinkID, userID, comment, datetime
FROM drinkComments
WHERE `drinkID` IN
( select distinct drinkID from drinkComments AS a where drinkComments.userID = 1)
AND drinkComments.dateTime > (
/*This gets the last date user commented on the main query's drinkID*/
select datetime FROM drinkComments WHERE drinkComments.userID = 1 AND drinkComments.drinkID = a.drinkID ORDER BY datetime DESC LIMIT 1
)
ORDER BY datetime DESC