I am trying to create a prepared statement with following code:
$statement = $db->prepare("SELECT * FROM myTable WHERE
STR_CMP(creationDate, $startingDate) = 1 ORDER BY creationDate DESC ");
I have also tried this:
$statement = $db->prepare("SELECT * FROM myTable WHERE
(creationDate > $startingDate) = 1 ORDER BY creationDate DESC ");
But I am getting null $statement. My goal is to get rows which it's creatrion date is newer than given date string. My creationDate column is stored as string.
I have logged my query string and it look like:
SELECT * FROM myTable WHERE (creationDate > 05.02.2015 14:08:31) = 1 ORDER BY creationDate DESC
What am I doing wrong?
1) You have to compare date to date.
SELECT * FROM myTable WHERE
DATE_FORMAT(STR_TO_DATE(creationDate, '%d.%m.%Y %H:%i:%s'),'%d.%m.%Y %H:%i:%s')
> '.$startingDate.' ORDER BY creationDate DESC
Tips
2) Store date in DB as date not string
3) Your date string seems to invalid?
Why not YYYYMMDD.
ORDER BY creationDate DESC may implict unexpected reulsts
I have solved my problem by trying my query string in my phpMyAdmin sql query section of my database. And I have relized when I build my query like this:
$statement = $db->prepare("SELECT * FROM myTable WHERE
(creationDate > $startingDate) = 1 ORDER BY creationDate DESC ");
It becomes:
SELECT * FROM myTable WHERE (creationDate > 05.02.2015 14:08:31) = 1
ORDER BY creationDate DESC
and MySql gives error for 05.02.2015 14:08:31
So I have prepared my statement like this:
$queryString = 'SELECT * FROM arendi_ideas WHERE (creationDate > "'.$startingDate.'") = 1 ORDER BY creationDate DESC';
$this->logger->debug("GetIdeasNewerThan: Query string = $queryString");
$this->statement = $this->db->prepare($queryString);
and it works fine.
Related
I've view ReportView
I want to fetch the result having date 10-Feb-2020
I tried
select * from ReportView where date = "10/02/2020";
select * from ReportView where date = date_format("10/02/2020","%d/%m/%Y");
select * from ReportView where date = str_to_date("10/02/2020","%d/%m/%Y");
select * from ReportView where date_format(date,"%d/%m/%Y") = date_format("10/02/2020","%d/%m/%Y");
select * from ReportView where str_to_date(date,"%d/%m/%Y") = str_to_date("10/02/2020","%d/%m/%Y");
select * from ReportView where date = CAST("2020-10-02" AS DATE);
select * from ReportView where CAST(date AS DATE) = CAST("2020-10-02" AS DATE);
Output is empty :
What actually is happening?
You need to use str_to_date() to parse the incoming dates, then date_format() to format them in the way you have it in the table column.
select * from ReportView where date = date_format(str_to_date('10/02/2020',"%d/%m/%Y"), '%d-%m-%Y');
In database; there's 3 columns, Name, CreatedDate and UpdatedDate.
I want to make stored procedure to to get all rows order last update or created date.
Here's my sample code.
SELECT * FROM tbl_name
ORDER BY CASE WHEN UpdatedDate != null THEN UpdatedDate ELSE CreatedDate END desc
But received wrong result. Any Idea?
When comparing with null you need the is operator.
SELECT * FROM tbl_name
ORDER BY CASE WHEN UpdatedDate is not null
THEN UpdatedDate
ELSE CreatedDate
END desc
or in your case you can just use
SELECT *
FROM tbl_name
ORDER BY coalesce(UpdatedDate, CreatedDate) desc
Im' trying to select all the rows with a concrete user and also on a specific date. My query always return "0", and this is no possible.
The column called "date-column" have a DATE format.
$my_date = '2015-01-21';
$q_search = $mysqli->query("SELECT *
FROM table_name
WHERE userID = ".$_SESSION['memberID']." AND
date_column = ".$my_date."");
echo 'number of results: '.$q_search->num_rows.';
Any ideas?
Insert query should be like this
$sql=" SELECT * FROM table_name WHERE userID = '$_SESSION[memberID]' AND date_column = '$my_date'";
You need to add ' in you date, like this
$q_search = $mysqli->query("SELECT * FROM table_name WHERE userID = ".$_SESSION['memberID']." AND date_column = '" . $my_date . "'");
You should quote the parameters:
$q_search = $mysqli->query("SELECT * FROM table_name WHERE userID = '".$_SESSION['memberID']."' AND date_column = '".$my_date."';");
I suggest you to sanitize the parameters with the function mysql_real_escape_string before concatenating them in the query, like that:
$q_search = $mysqli->query("SELECT * FROM table_name WHERE userID = '".mysqli_real_escape_string($_SESSION['memberID'])."' AND date_column = '".mysqli_real_escape_string($my_date)."';");
This query is to retrieve some messages, it retuns all of them:
$q = "
SELECT *
FROM pms
WHERE
(
(id_to = $id and id_from = ".sesion().")
OR
(id_from = $id and id_to = ".sesion().")
)
AND (id > $from)
ORDER by fecha ASC";
The thing is i would like to get the last 50 elements, but I think its ony posible using DESC ordering..
how can i do it?
Do i need to count first how many rows so then can I use LIMIT $many-$ipp,$many ? or is there a way to invert the result order?
Just make your query a subquery:
SELECT * FROM
(
SELECT *
FROM pms
WHERE
(
(id_to = $id AND id_from = ".sesion().")
OR (id_from = $id and id_to = ".sesion().")
)
AND id > $from
ORDER BY fecha DESC
LIMIT 50
) q1
ORDER BY fecha ASC
I have this query
SELECT bul.id
FROM bul
WHERE id IN (SELECT hotid AS parentid
FROM likehot
WHERE hotid IN (SELECT id
FROM bul
WHERE DATE >= '1315976410')
GROUP BY hotpid
ORDER BY COUNT( hotid ) DESC )
when I runt this inner query
SELECT id
FROM bul
WHERE DATE >= '1315976410')
GROUP BY hotpid
ORDER BY COUNT( hotid ) DESC
I get
parentid
3655
3656
3622
3644
and when I run whole query I get
parentid
3656
3655
3622
3644
I really don't understand why the order of the ids change?
SOLUTION :-
<?php
$query_hotpress_like = "SELECT hotid AS parentid FROM likehot WHERE hotid IN (SELECT id FROM bul WHERE DATE >= '" . (time() - (24 * 60 * 60)) . "') GROUP BY hotid ORDER BY COUNT( hotid ) DESC";
$exe_hotpress_like = execute_query($query_hotpress_like, true, "select");
$temp_like1 = array();
foreach ($exe_hotpress_like as $kk => $exe_like) {
$temp_like1[] = "'" . $exe_like['parentid'] . "'";
}
$temp_like = str_replace(",''", "", implode(',', $temp_like1));
$query_hotpress = "SELECT bul.id,photo_album_id,eventcmmnt_id,link_url,youtubeLink,link_image,id, mem_id, subj, body, bul.date,parentid, from_id, visible_to,image_link,post_via FROM bul WHERE id IN ($temp_like) ORDER BY FIELD(id,$temp_like ) LIMIT 5";
?>
execute_query() is the inbuilt function to get the result of query.
That happens because for IN operator order doesn't matter.
If you need to sort outer query - sort outer query.
Since you didn't specify an order for the "whole" query, the database is at liberty to return rows in any order it wants. The specific order you get is a result of how looking up rows is done when using the IN operator.
On your other query you are specifying an order yourself, so the database has to honor it.