Mysql limit function doesn't seem to work for me - mysql

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

Related

Convert SQLITE to MYSQL

Can someone help me convert this into MYSQL since IN is not supported in MYSQL . Should i use INNER JOIN? but how?
DELETE from SSLDOMAINS_logstable where id IN (SELECT id from SSLDOMAINS_logstable order by id asc limit 50)
You only need the ORDER BY clause and LIMIT in MySql:
DELETE FROM SSLDOMAINS_logstable
ORDER BY id
LIMIT 50
This query will delete the first 50 rows of the table ordered by id ascending.
DELETE t1.*
FROM SSLDOMAINS_logstable t1
JOIN ( SELECT t2.id
FROM SSLDOMAINS_logstable t2
ORDER BY id ASC
LIMIT 50 ) t3 ON t1.id = t3.id
If id is unique (including primary), see forpas's solution. But if not... subquery selects least 50 id values, query deletes records with these id values - i.e. LIMIT in this case works like "LIMIT WITH TIES"

How do I select the last but 10 rows from a mysql table

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;

Return Rows Random and then Order By

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

MySql if then query

Is it possible to use if/then statements in mySql? I am looking to check the value of a column in a JOIN table and order the results based on the value of that column.
My sorry attempt:
SELECT t1.*
FROM t1
JOIN t2 ON t1.ID = t2.ID
WHERE t1.ID = '888'
if(t2.col1 = '1') ORDER BY t1.tid ASC
else ORDER BY RAND()
You can use CASE:
ORDER BY
CASE WHEN t2.col1 = 1 THEN t1.tid ELSE rand() END ASC
Beware the performance of this may not be so good, as MySQL won't be able to use an index for the order t1.tid.
You should also be aware that you can order by multiple things, so that possibly this will do what you want:
ORDER BY t1.tid ASC, RAND()
Also, you should keep in mind that ORDER BY RAND() LIMIT 1 will actually fetch every row, calculate a random number for each, sort them, then finally just return the first row. So on data of a reasonable size, it will be slow (and also result in temp tables).

Join statement...how do do ORDER BY?

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.