I have this query that returns the results by ordering it by focus.name ASC. I would like to expand on this by first ordering it by rand() then ordering it by focus.name and limiting it by 10.
SELECT * FROM vendor_products WHERE
vendor_products.focus_id IN (SELECT focus.id FROM focus WHERE
focus.name=? AND
mydelete='0' ORDER BY focus.name ASC) AND product_id=?
AND mydelete='0' ORDER BY focus_id ASC
do something like this
select t1.*
from table t1, (select id from table order by rand() limit 10) t2
where t1.id = T2.id
order by t1.name
see here
Related
I have the following SQL query : "SELECT * FROM table WHERE id = '$geo_name_id' ORDER BY timestamp DESC LIMIT 10 OFFSET $offset"
This works fine, but now I want to also return the amount of rows in the whole table WHERE id = 'geo_name_id (without the limit). I have tried something like this :
"SELECT COUNT(*) as totalExpectedItems, * FROM story_img_vid WHERE id = '$geo_name_id' ORDER BY timestamp DESC LIMIT 10 OFFSET $offset"
However this doesn't seem to work as expected as it only returns one row.
Use a subquery :
"SELECT t.*,
(SELECT COUNT(*) FROM table
WHERE id = '$geo_name_id') as cnt
FROM table t
WHERE t.id = '$geo_name_id'
ORDER BY t.timestamp DESC
LIMIT 10 OFFSET $offset"
cnt Column will contain the count.
Or a join to a derived table:
"SELECT t.*,
t2.cnt
FROM table t
JOIN(SELECT s.id,count(*) as cnt
FROM table s GROUP BY s.id) t2
ON(t.id = t2.id)
WHERE t.id = '$geo_name_id'
ORDER BY t.timestamp DESC
LIMIT 10 OFFSET $offset"
Do it like this:
"SELECT COUNT(*) as totalExpectedItems, * FROM story_img_vid WHERE id = '$geo_name_id'"
I want to run this query
select *
from table
order asc
limit N;
where N is the total number of rows minus 10.
SELECT COUNT(*) FROM (SELECT * FROM table)
returns the total as 189 so, in this case, I would want my limit to be 179
If order is not important, you can use the offset of limit:
Note, there is no actual value for 'Until End Of Table'. The MySQL Documentation suggests to use "some large number" for the second parameter.
SELECT *
FROM table1
order by ID DESC
LIMIT 10, 999999999999999
If you do want in in ascending order you can apply a different ordering afterwards:
SELECT
*
FROM
(SELECT *
FROM table1
ORDER BY ID DESC
LIMIT 10, 999999999999999) x
ORDER BY
ID ASC
Not sure if the most efficient one, but this should work if you only have one field as a primary key.
select *
from T1
where
T1.id not in (
select top(10) id
from T1
order by id desc
)
order by
id;
It will get the last rows by your order and then you can exclude by the key.
Edit:
Better yet, instead of not in, you can use a left outer join.
select
T1.*
from T1
left outer join ( select top(10) id from Enums_Tables order by id desc ) as T2
on
T1.id = T2.id
where
T2.id is null;
I have 2 tables. I get result in order by desc format.
Now I want to display info in that same order format. But I am not able to do that.
select *
from table1
where field in (select *
from table2
where StartDate > '2011-11-01'
AND StartDate < '2011-11-30'
group by field1
order by count(field1) desc );
The inner query is ordered in descending, but when used with the outer query, the order is lost.
An in clause does not preserve ordering, I'm surprised MySQL even allows it like that :)
One solution is to calculate the count in a subquery, and use that to order:
select *
from table1 t1
join (
select field1
, count(field1) as Field1Count
from table2
where StartDate > '2011-11-01'
and StartDate < '2011-11-30'
group by field1
) t2
on t1.field1 = t2.field1
order by
t2.Field1Count desc
I have the following LEFT JOIN statement (though probably also applies to even a simpler SELECT statement):
SELECT * FROM t1 LEFT JOIN t2 ON t2.c = t1.c WHERE t1.m LIKE 'captain%' GROUP BY
t1.c ORDER BY t2.date DESC LIMIT 0,10
The results get returned but they are not ordered by t2.date DESC...I imagine this is due to having the GROUP BY statement in there. How can I group the results AND order them?
thx
Put your query in a subquery and then use ORDER BY.
SELECT *
FROM
(SELECT *
FROM t1 LEFT JOIN t2 ON t2.c = t1.c
WHERE t1.m LIKE 'captain%'
GROUP BY t1.c DESC LIMIT 0,10) l1
ORDER BY date
In normal SQL it would not even be allowed to ORDER BY t2.date because you aren't grouping on it and you are not using an aggregate function on it, so you can't select it.
Anyway, as MySQL just picks values from the rows in an undefined manner and afterwards sorts them, the t2.date column in your results should be sorted. Please show some actual output.
Here is my query,
select t1.dSyllabus_id,t1.dBatch,t1.dFilePathName,
t2.dDegreeName,t3.dDepartmentAbbr
from tbl_syllabus as t1
join tbl_degree_master as t2,
tbl_department_master as t3
where t2.dDegree_id=t1.dDegree_id
and t3.dDepartment_id=t1.dDepartment_id
and t1.dCollege_id='1'
and t1.dIsDelete='0'
and i get
Without Limit http://img534.imageshack.us/img534/2165/withoutlimi.jpg
applying limit ,
select t1.dSyllabus_id,t1.dBatch,t1.dFilePathName,
t2.dDegreeName,t3.dDepartmentAbbr
from tbl_syllabus as t1
join tbl_degree_master as t2,
tbl_department_master as t3
where t2.dDegree_id=t1.dDegree_id
and t3.dDepartment_id=t1.dDepartment_id
and t1.dCollege_id='1'
and t1.dIsDelete='0'
limit 0,5
i get ,
With Limit http://img13.imageshack.us/img13/2470/withlimit.jpg
I dont get the first five records why?
You get 5 records, but you haven't set an order. They will not come back in a specific order unless you tell them too. In your case I believe you want them in order of their id. Add something like:
order by `t1`.`dSyllabus_id` ASC
So your query looks like:
select t1.dSyllabus_id,t1.dBatch,t1.dFilePathName,
t2.dDegreeName,t3.dDepartmentAbbr
from tbl_syllabus as t1
join tbl_degree_master as t2,
tbl_department_master as t3
where t2.dDegree_id=t1.dDegree_id
and t3.dDepartment_id=t1.dDepartment_id
and t1.dCollege_id='1'
and t1.dIsDelete='0'
order by `t1`.`dSyllabus_id` ASC
limit 0,5
Use ORDER BY
and t1.dIsDelete='0'
ORDER BY 't1.dSyllabus_id'
limit 0,5