Is there any method to solve this problem ?
in mysql column field date is value as timestamp and i want to fetch all row date which has specific value as format Y-mm ( 2016-05 )?
please help me i am stacking in this problem, Is it possible ?
select * from article where postby = 'admin' AND substr(`date`,0,7) = '2016-05'
Try 'date_format()' mysql function:
select * from article where postby = 'admin' AND date_format(`date`,'%Y-%m') = '2016-05'
try to CAST it first, if you have to use SUBSTR
SELECT * FROM article WHERE postby = 'admin'
AND SUBSTR(CAST(`date` AS CHAR(10)),0,7) = '2016-05'
Related
I have table with 10 columns and I want to check input value in where clause of the MySQL query.
I want to do something like this. But, when I use this query I am getting an error.
for example :
SELECT * FROM user_data
where poll_title='$poll_title'
and '$voter' IN (user_vote_1,user_vote_2,user_vote_3...user_vote_10)
order by idpoll ASC
user_vote_1 to 10 (value is null'ed in the database) and I want to retrieve only that rows from a column which have $voter value.
I think you need this comparison (Not Sure OfCourse) :-
SELECT * FROM user_data
where poll_title = "$poll_title"
and (user_vote_1 = "$voter"
OR user_vote_2 = "$voter"
OR user_vote_3 = "$voter"
OR user_vote_4 = "$voter"......OR user_vote_10 = "$voter")
order by idpoll ASC
If I've understood what you want to do - return only the column with the value - then would coalesce do the job? This assumes that the value in user_vote_n will either match the value you're looking for or be null, since coalesce returns the first non-null argument.
(untested)
select coalesce(user_vote_1, user_vote_2, user_vote_3, ) as UserVote from user_data
where coalesce(user_vote_1, user_vote_2, user_vote_3, ) = '$voter';
That aside, this looks like a structure that could do with normalising - a single 'user_vote' column and a single 'user_vote_number' column.
SELECT * FROM (`poll`) WHERE `poll_id` IN ('6,10,5,9,1') AND `poll_status` = '1'
I tried this query. I expected It would give result as I gave. But It sorting the result automatically in ascending. I need result as I give. Kindly give me a solution. Thanks in advance.
You can use FIELD in mysql:
SELECT *
FROM (`poll`)
WHERE `poll_id` IN (6,10,5,9,1)
AND `poll_status` = 1
ORDER BY FIELD(`poll_id`,6,10,5,9,1)
You have to separate your ids like that:
SELECT * FROM (`poll`) WHERE `poll_id` IN (6,10,5,9,1) AND `poll_status` = '1'
I am going to give order in query like:
SELECT * FROM `sales_report` ORDER BY `cancel_date`, `pur_date`
But its not working could you please check what is wrong in this query because its not ordering dates?
pur_date type is date and cancel_date type is text so is it issue ? I can't change its type.
I echo a different think like:
if(cancel_date != ''){
$transection_date = cancel_date;
}
else {
$transection_date = pur_date;
}
Try to use STR_TO_DATE() function:
SELECT
*
FROM
`sales_report`
ORDER BY
STR_TO_DATE(`cancel_date`, '%Y-%m-%d'), `pur_date`;
As zerkms mentioned in comments: You should use format specifiers, dependent of your text values in cancel_date column.
SELECT *, CAST(`cancel_date` as DateTime) cancelDate
FROM `sales_report`
ORDER BY `cancelDate`, `purDate`
This should work
Here is my current MySQL syntax (but this doesn't work):
SELECT * FROM posts WHERE userID = $userID AND postBy = $postBy AND postType = 'unread'
Here is the picture of my MySQL table (a picture can show more than words!)
I want to know if there is any other way of doing it or if I have to forget this idea?
There's nothing wrong with what you're attempting to do, it's just that you haven't quoted the PHP variable
Make sure you're escaping/sanitizing your input. Also use prepared statements if possible
SELECT * FROM posts WHERE userID = $userID AND postBy = '{$postBy}' AND postType = 'unread'
You should really use parameters, but other than that you will need to enclose the text variables in single quotes.
SELECT * FROM posts WHERE userID = $userID AND postBy = '$postBy' AND postType = 'unread'
I have a query that I am using to get results that are newer than a given timestamp. It is supposed to find all those newer messages that were sent and received by a given user. The problem is, no matter what I change the timestamp parameter to, I even changed it to 'g', I still get 4 results. The timestamp doesn't seem to alter my query. In the table, the timestamp is an int(11). I tried using intval and defining the bindvalue as a param int and still no luck. Any ideas?
$sql = 'SELECT timestamp, user, message, receiver, convo_id
FROM chatbox
WHERE user = ?
OR receiver = ?
AND timestamp = ?
ORDER BY timestamp DESC';
$stmt = $conn->prepare($sql);
$stmt->bindValue(1,'username');
$stmt->bindValue(2,'username');
$stmt->bindValue(3,'g');
$stmt->execute();
I suspect it's the precedence of AND and OR in your where clause. Try using parentheses:
WHERE ( user = ? OR receiver = ? )
AND timestamp = ?
assuming that's what you need.
Shouldn't you do this instead in your $sql variable?
AND timestamp > ?