How can I ORDER BY a string which has ID at the end of the string?
For Example, I want to order these Strings by the numbers at the end (after RSPP):
RSPP891
RSPP896
RSPP897
RSPP898
RSPP899
RSPP900
RSPP901
RSPP902
RSPP903
RSPP904
RSPP730
RSPP731
RSPP1380
RSPP733
RSPP734
I read something about substringing so i tried this query instead, but without any success.
SELECT `RsppTags` FROM Table WHERE ConnectionID = 15202
ORDER BY
SUBSTR(RsppTags FROM 4 FOR LENGTH(RsppTags)-1),
CAST(SUBSTR(RsppTags FROM 1) AS UNSIGNED)
Is it possible to get this data ORDER BY ASC?
SELECT RsppTags FROM Table
WHERE ConnectionID = 15202
ORDER BY CAST(SUBSTR(RsppTags FROM 5) AS UNSIGNED)
By default MYSQL provide ASC ordering.
Try above query.
It will help you.
Try:
ORDER BY CAST(SUBSTR(RsppTags FROM 5) AS UNSIGNED) ASC
Sample SQL Fiddle showing the result
order by SUBSTR(RsppTags, 5) + 0
Try
SELECT `RsppTags` FROM Table WHERE ConnectionID = 15202
ORDER BY CAST(SUBSTR(RsppTags, 5) AS UNSIGNED) ASC;
SELECT `RsppTags`
FROM Table WHERE ConnectionID = 15202
ORDER BY
SUBSTR(RsppTags,1,4),
CAST(SUBSTR(RsppTags,5) AS UNSIGNED);
Related
there is an in parameter as page. all i want to do is expel 1 from it and multiply by 10. but it gives me an error every time.
IF !a THEN
SELECT *
from entry
WHERE topic_foreign_id = (
select topic_id
from topic
where topic_name = topicName
)
ORDER BY entry_time ASC
LIMIT 10 OFFSET page;
ELSE
SELECT *
from entry
WHERE topic_foreign_id = (
select topic_id
from topic
where topic_name = topicName
)
ORDER BY entry_time ASC;
END IF
this lines of code works great but when i want to make an aritmetic operation in first SQL query myAdmin throws an error everytime.
SELECT *
from entry
WHERE topic_foreign_id = (
select topic_id
from topic
where topic_name = topicName
)
ORDER BY entry_time ASC
LIMIT 10 OFFSET 10 * ( page - 1); //throws error
--- EDITED ---
LIMIT doesn't seem to like calculations. Try using a variable and use that value in your stored procedure
DECLARE v_page_offset INT DEFAULT 0;
SET v_page_offset = 10*(page-1);
Then later
LIMIT 10 OFFSET v_page_offset;
This is Query and i am Unable to find the solution for this An alias was previously found near "req_id". Why this error is happening? Please review it.
select property.property_type,
property.agent_id,
agents_profile.user_photo,
agents_profile.phone_no,
agents_profile.first_name,
agents_profile.family_name,
agents_profile.mob_verify_flag,
property.location,
property.longitude,
property.latitude,
property.images,
property.floor_plan_image,
property.epc_image,
property.available_start_date,
property.available_end_date,
property.monthly_rent,
property.deposit,
property.bedrooms,
property.bathrooms,
property.furnished_type,
property.is_secure_access,
property.is_above_street_level,
property.is_parking,
property.is_parking_covered,
property.total_parking,
property.is_parking_secure,
property.is_garden,
property.accept_social_housing_tenant,
property.flag,
property.isWifiAvailable,
property.isPetsPresent,
property.isSmoking,
property.isStudent
(SELECT id
FROM `request_property`
WHERE tenant_id=0 AND agent_id=property.agent_id AND property_id=294
LIMIT 1) as req_id
FROM property
INNER JOIN agents_profile ON property.agent_id=agents_profile.agent_id
WHERE property.id=294
LIMIT 0, 25`
You're missing a comma after isStudent:
// more stuff above
property.isSmoking,
property.isStudent, -- HERE
(SELECT id
FROM `request_property`
WHERE tenant_id=0 AND agent_id=property.agent_id AND property_id=294
LIMIT 1) as req_id
FROM property
There is no comma separating the subquery field definition from the list of fields:
select property.property_type,
...
property.isStudent, <---this comma was missing
(SELECT id
FROM `request_property`
WHERE tenant_id=0 AND agent_id=property.agent_id AND property_id=294
LIMIT 1) as req_id
FROM property
INNER JOIN agents_profile ON property.agent_id=agents_profile.agent_id
WHERE property.id=294
LIMIT 0, 25`
I have the below query in mysql, which sorts rows in ascending order of rm.meta_val. But in addition I want the null meta_val rows to be positioned at the last.
Could anyone help out?
SELECT rt.tax_id,ra.*,
FROM req_tax rt, req_aspects ra, req_meta rm
where ra.aspect_id = rt.req_aspects_id
and rt.tax_id=rm.req_tax_id
and rm.req_id = rt.req_id and rt.requests_id = 18
group by rt.tax_id
ORDER BY rm.meta_val asc;
Use IF in the ORDER BY clause:
ORDER BY
IF(rm.meta_val IS NULL, 1, 0),
rm.meta_val asc;
You can sort on a function result, so use an if to check whether the value is null, and if so return a very high value, otherwise return the value. Ideally, you would choose a value of the same type (number of bits, signed vs unsigned) based on the Max value being sorted plus one, as this avoids type promotion, but that may not be important to you. Switch the if returns if you want the nulls on the other end
SELECT rt.tax_id,ra.*,
FROM req_tax rt, req_aspects ra, req_meta rm
WHERE ra.aspect_id = rt.req_aspects_id
AND rt.tax_id=rm.req_tax_id
AND rm.req_id = rt.req_id
AND rt.requests_id = 18
GROUP BY rt.tax_id
ORDER BY if(rm.meta_val is NULL, 0xFFFFFFFF, rm.meta_val) asc;
try with case:
SELECT rt.tax_id,ra.*,
FROM req_tax rt, req_aspects ra, req_meta rm where ra.aspect_id = rt.req_aspects_id
and rt.tax_id=rm.req_tax_id and rm.req_id = rt.req_id and rt.requests_id = 18 group by rt.tax_id
order by case when rm.meta_val is null then 1 else 0 end, rm.meta_val
Try this query use ISNULL:-
order by ISNULL(rm.meta_val), rm.meta_val ASC
Your query like that :-
SELECT rt.tax_id,ra.*,
FROM req_tax rt, req_aspects ra, req_meta rm where ra.aspect_id = rt.req_aspects_id
and rt.tax_id=rm.req_tax_id
and rm.req_id = rt.req_id and rt.requests_id = 18 group by rt.tax_id order by ISNULL(rm.meta_val), rm.meta_val ASC;
I am using this ORDER BY clause in my MySQL query:
"ORDER BY FIELD(best_id.status, 'open', 'bekeken', 'verstuurd', 'binnen'), best_id.datum_leveren ASC
Works fine.
Now I would like to make an difference with sorting the rows, ASC combined with DESC. I have been trying to achieve this by change the clause to:
ORDER BY FIELD(best_id.status, 'open', 'bekeken', 'verstuurd', 'binnen'),
CASE WHEN best_id.status = 'binnen' THEN best_id.datum_leveren DESC, END
ELSE best_id.datum_leveren ASC END";
But I can't get it tow work. What is the right way for this or is it impossible to use an CASE in the ORDER BY clause?
Any help is much appreciated.
You can use
ORDER BY CASE
WHEN best_id.status = 'binnen'
THEN best_id.datum_leveren
ELSE NULL
END DESC,
CASE
WHEN best_id.status = 'binnen'
THEN NULL
ELSE best_id.datum_leveren
END ASC
How can I group a query result by a field that is not saved in the database.
For example I want to group the result by duration which is came from subtraction of start time and end time.
here is how i find out the duration
date1= $row_TicketRS['CloseDate'];
$date2 = $row_TicketRS['OpenDate'];
$diff = abs(strtotime($date2) - strtotime($date1));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
if ( $days > 0)
{
$time1 = $row_TicketRS['OpenTime'];
$time2= $row_TicketRS['CloseTime'];
$t1=($time1);
$t2=($time2);
$end=('14:30');
$start=('07:30');
$n = $end- $t1;
$n2 = $t2- $start;
$Hours2 = floor(($n+$n2)+(($days-1)*7));
echo $Hours2.' Hours';
but know i do not know how to add it to the query
here is my query
$strQuery = "SELECT count(`ticket`.TicketID) as TotOutput, department.`DeptName` FROM `ticket`, `user`, department where ticket.OwnerID = user.EmpNo and user.`DepartmentID` = department.`DepartmentID` and OpenDate between'".$DateFrom."' And '".$DateTo."'"
It'd be better to have details, but a derived table/inline view would allow you to group by a computed value:
SELECT x.duration,
COUNT(*)
FROM (SELECT t.col,
t.end_time - t.start_time AS duration
FROM YOUR_TABLE t) x
GROUP BY x.duration
How about adding that computed value to the query with an alias like this:
SELECT some_fields, end - start AS duration FROM table ORDER BY duration
dont put alias for hidden column , use directly
exmaple:
SELECT id, FLOOR(value/100)
FROM tbl_name
GROUP BY id, FLOOR(value/100);
Reference
MySQL permits expressions in GROUP BY
clauses, so the alias is unnecessary: