I have numbers like this: 666/2014, 01/2014, 1/2014, 02/2014, 125/2014, 06/2014 ...etc as STRING named 'n_inscription' in database.
I want to retrieve those strings In ORDER from database
I used this:
$sql_students = $bd->query("SELECT * FROM `es_student_infos`
WHERE school_year='$school_year'
ORDER BY right(n_inscription, 4) * 1,
substring_index(n_inscription, '/', 1) * 1");
I get result like this:
01/2014,
02/2014,
06/2014,
1/2014,
125/2014,
666/2014
and the result I'm looking for is like this:
01/2014,
1/2014,
02/2014,
06/2014,
125/2014,
666/2014
any suggestion please?
The best approach is probably to normalize the input so normal sorting does what you want it to do. For example, store the student number and year in two separate INTEGER columns and then ORDER BY studentNumber ASC, inscriptionYear ASC.
If that's absolutely not possible:
SELECT
*
FROM
es_student_infos
ORDER BY
CAST(RIGHT(n_inscription, 4) AS UNSIGNED) ASC,
CAST(LEFT(n_inscription, LOCATE('/', n_inscription) - 1) AS UNSIGNED) ASC
Link to fiddle demonstrating the solution: http://sqlfiddle.com/#!2/a5538/1/0
The correct way is change the type of n_inscription to date/datetime/timestamp and then use order by as default.
There's more advantages to use date fields like comparison and date calculations, so I suggest you do like this.
EDIT: Change the order by collumn to school_year and not n_inscription:
SELECT * FROM `es_student_infos`
WHERE school_year='$school_year'
ORDER school_year
Related
i have this query :
SELECT *
FROM news
WHERE STATE LIKE 'SI'
AND data<'".time()."'
ORDER
BY data DESC
limit 0,1
and i would like to know if the function time it's correct because there is an error on synthase.
thank's you !
There are reserved words in MySQL which you cannot use as column names without clearly indicating they are names. See:
https://dev.mysql.com/doc/refman/5.7/en/keywords.html
Both 'data' and 'date' are reserved words. Use back ticks to indicate they are used as names:
$query = "SELECT *
FROM `news`
WHERE `STATE` LIKE 'SI'
AND `data` < '".time()."'
ORDER
BY `data` DESC
LIMIT 0,1
or better, in my opinion, use better column names:
$query = "SELECT *
FROM newsItems
WHERE itemState LIKE 'SI'
AND creationDate < '".time()."'
ORDER
BY creationDate DESC
LIMIT 0,1";
As you can see I had to guess what the columns really stand for, because it's not directly clear from the names. It should be, because that's what they are there for.
TIME() is a function in which you also need to pass your parameter.
So, instead of using it blank like:
select TIME();
you need to use it in this way:
select TIME(now());
Note: In your query, you need to pass like (only if you have datetime field in your table):
AND time(data) < time(now())
I have made a search bar for searching string my query is
$q = $_GET('val');
$sqlquery=$mysqli->query("SELECT * FROM tickets_search WHERE (ticket_date LIKE
'%".$q."%') OR (car_number LIKE '%".$q."%') order by ticket_id desc");
Now when I search for HU-GG 6 it gives me two results
see here:
Snippet-1 : HU-GG 6-1
When I search for HU-GG-6.it gives me another result
see here:
Snippet-2: HU-GG-6-2
What I want that is to show all three results with HU-GG-6 or HU-GG 6 whatever I write.It should show me all three results.
TRY THIS: You can use REPLACE to satisfy both of your conditions but I am not sure how much it will help and please adjust REPLACE in PHP if it's not correct:
SELECT *
FROM tickets_search
WHERE (ticket_date LIKE '%".$q."%') OR
(REPLACE(car_number, ' ', '-') LIKE REPLACE('%".$q."%', ' ', '-'))
ORDER BY ticket_id DESC
I do want to sort my results from different datetimes, like this ("SELECT * FROM table ORDER BY datetime_1 and datetime_2 and datetime3 DESC");
is that possible ??
If you use SQL to get your posts, you can try this:
SELECT * FROM your_posts_table ORDER BY your_date_field DESC
In some situations, you may just add
ORDER BY your_date_field DESC
after your SQL.
if you using ORM, you have to look at the document.
your overwriting while($users->results() as $users) { $users variable
so you have to do something like this
while($users->results() as $users_data) {
echo $user_data->name;
}
$active_sth = $dbh->prepare("SELECT * FROM user_campaign
WHERE status='blasting'
OR status='ready'
OR status='followup_hold'
OR status='initial_hold'
AND uid=:uid
ORDER BY status ASC");
$active_sth->bindParam(':uid', $_SESSION['uid']['id']);
$active_sth->execute();
I am positive $_SESSION['uid']['id'] = 7
but it will also pull results of id 10 or any other number.
Is my AND/OR clause written wrong?
Yes, query is wrong
SELECT * FROM user_campaign
WHERE (
status='blasting'
OR status='ready'
OR status='followup_hold'
OR status='initial_hold'
)
AND uid=:uid
ORDER BY status ASC
You have to group all ORs to make sure that row got one of this values, and separately check if it have given uid.
The proper way to write that is:
SELECT * FROM user_campaign
WHERE status IN ('blasting', 'ready', 'followup_hold', 'initial_hold')
AND uid =: uid
ORDER BY status ASC
You should use IN instead of that huge amount of ORs :)
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