mysql limit not work orderby with limit 0,5 - php

$selectorders=sprintf("SELECT s.stud_rollno, s.admissiondate, s.fname, s.lname, s.gender, c.communityname, t.name, y.yearname, s.iname
FROM erp.student s inner join year y
on year_id = s.ayear
inner join community c
on c.d_id = s.community
inner join types t
on t.id = s.department
ORDER BY s.stud_rollno
limit 0,10");
$results = mysql_query($selectorders) or die(mysql_error());
$tot_rsselect = mysql_num_rows($results);
my runtime error is
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 0, 200' at line 1
but run in localhost/phpmyadmin page.

I think you are trying to select only top 10 rows. So in this scenario there is no need to use 0, 10 or like somthing. You can just simply write LIMIT 10
So your query will be:-
"SELECT s.stud_rollno, s.admissiondate, s.fname, s.lname, s.gender, c.communityname, t.name, y.yearname, s.iname
FROM erp.student s inner join year y
on year_id = s.ayear
inner join community c
on c.d_id = s.community
inner join types t
on t.id = s.department
ORDER BY s.stud_rollno
limit 10";
I'm not sure but this might be your solution.

Related

Mysql query error limit

I write a website and use php. When ı write
$cat = mysql_query("
Select m.Image_link AS link
, m.Name
, m.ID
, c.Name AS catName
from movie m
join has_category mc
on m.ID = mc.movie_id
join category c
on c.ID = mc.category_id
where c.Name = '$category'
ORDER
BY ID desc"
);
this query everything is okay but when ı insert limit, ı take error.
#$sayfa=$_GET['s'];
$kacar=12;
$toplansayfa=ceil(mysql_num_rows($cat)/$kacar);
$baslangic=($sayfa * $kacar) - $kacar;
$bul_cat=mysql_query("
Select m.Image_link AS link
, m.Name
, m.ID
, c.Name AS catName
from movie m
join has_category Mc
on m.ID = mc.movie_id
join category c
on c.ID = mc.category_id
where c.Name = '$category'
ORDER
BY ID desc
limit $baslangic, $kacar
");
echo mysql_error();->
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-12, 12' at line 1
Where is my mistake..thanks for your interest.
As with many SQL programming errors, it helps a great deal to look at the actual text of the query you tried to run.
So, try this:
$query = "Select movie.Image_link AS link, movie.Name, movie.ID, category.Name AS catName from (movie inner join has_category on movie.ID=has_category.movie_id inner join category on category.ID=has_category.category_id) where category.Name='$category' ORDER BY ID desc limit $baslangic, $kacar";
$bul_cat = mysql_query($query); /*deprecated API! */
if (!$bul_cat) {
echo "query failed: " . $query;
echo mysql_error(); /* deprecated API! */
}
You will almost certainly spot the problem right away when you display the actual query you tried to run. I'm sure Paul Siegel's diagnosis is correct... your query says LIMIT -12,12. You Can't Do That™.

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use

The Error of mysql is following.
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'message.msg_type=R GROUP BY ticket.ticket_id ORDER BY
ticket.created DESC LIMIT ' at line 1
The query is following.
SELECT DISTINCT
ticket.ticket_id,lock_id,ticketID,ticket.dept_id,ticket.staff_id,subject,ticket.name,ticket.email,dept_name,lastresponse
,ticket.status,ticket.source,message.message,isoverdue,isanswered,ticket.created,pri.*
,count(attach.attach_id) as attachments
,staff.firstname,staff.lastname , IF(ticket.reopened is
NULL,ticket.created,ticket.reopened) as effective_date FROM
kt_ticket ticket LEFT JOIN kt_department dept ON ticket.dept_id =
dept.dept_id LEFT JOIN kt_priority pri ON
ticket.priority_id=pri.priority_id LEFT JOIN kt_ticket_lock tlock ON
ticket.ticket_id=tlock.ticket_id AND tlock.expire>NOW() LEFT JOIN
kt_ticket_attachment attach ON ticket.ticket_id=attach.ticket_id
LEFT JOIN kt_ticket_message message ON
(ticket.ticket_id=message.ticket_id ) LEFT JOIN kt_staff staff ON
ticket.staff_id=staff.staff_id WHERE 1=1 AND status='open' AND
ticket.pp_group =1 message.msg_type=R GROUP BY ticket.ticket_id
ORDER BY ticket.created DESC LIMIT 0,25
There is a missing AND clause between following two conditions.
ticket.pp_group =1
AND
message.msg_type=R
Also the R should be quoted with single quotes, as you are comparing chracter.
So that would look something like this,
AND ticket.pp_group =1 AND message.msg_type='R'
The strings should be quoted and a missing 'AND'
[...] AND message.msg_type='R' [...]
The error message points you to that direction, even though it is a bit vague.
You forgot to write and between two where condition
SELECT DISTINCT ticket.ticket_id,
lock_id,
ticketID,
ticket.dept_id,
ticket.staff_id,
subject,
ticket.name,
ticket.email,
dept_name,
lastresponse,
ticket.status,
ticket.source,
message.message,
isoverdue,
isanswered,
ticket.created,
pri.*,
count(attach.attach_id) AS attachments,
staff.firstname,
staff.lastname,
IF(ticket.reopened IS NULL,ticket.created,ticket.reopened) AS effective_date
FROM kt_ticket ticket
LEFT JOIN kt_department dept ON ticket.dept_id = dept.dept_id
LEFT JOIN kt_priority pri ON ticket.priority_id=pri.priority_id
LEFT JOIN kt_ticket_lock tlock ON ticket.ticket_id=tlock.ticket_id
AND tlock.expire>NOW()
LEFT JOIN kt_ticket_attachment attach ON ticket.ticket_id=attach.ticket_id
LEFT JOIN kt_ticket_message message ON (ticket.ticket_id=message.ticket_id)
LEFT JOIN kt_staff staff ON ticket.staff_id=staff.staff_id
WHERE 1=1
AND status='open'
AND ticket.pp_group =1 AND message.msg_type=R
GROUP BY ticket.ticket_id
ORDER BY ticket.created DESC LIMIT 0, 25;
Please use below query
SELECT DISTINCT ticket.ticket_id,lock_id,ticketID,ticket.dept_id,ticket.staff_id,subject,ticket.name,ticket.email,dept_name,lastresponse ,ticket.status,ticket.source,message.message,isoverdue,isanswered,ticket.created,pri.* ,count(attach.attach_id) as attachments ,staff.firstname,staff.lastname , IF(ticket.reopened is NULL,ticket.created,ticket.reopened) as effective_date FROM kt_ticket ticket LEFT JOIN kt_department dept ON ticket.dept_id = dept.dept_id LEFT JOIN kt_priority pri ON ticket.priority_id=pri.priority_id LEFT JOIN kt_ticket_lock tlock ON ticket.ticket_id=tlock.ticket_id AND tlock.expire>NOW() LEFT JOIN kt_ticket_attachment attach ON ticket.ticket_id=attach.ticket_id LEFT JOIN kt_ticket_message message ON (ticket.ticket_id=message.ticket_id ) LEFT JOIN kt_staff staff ON ticket.staff_id=staff.staff_id WHERE 1=1 AND status='open' AND ticket.pp_group = 1 AND message.msg_type=R GROUP BY ticket.ticket_id ORDER BY ticket.created DESC LIMIT 0,25
Two problems:
first there is an AND missing: ticket.pp_group =1 AND message.msg_type=R GROUP BY ticket.ticket_id ORDER BY ticket.created DESC LIMIT 0,25
second strings must be quoted by single quote: message.msg_type='R'
So the last part must be:
ticket.pp_group =1 AND message.msg_type='R' GROUP BY ticket.ticket_id ORDER BY ticket.created DESC LIMIT 0,25

Left Join After Where Clause

I'm having trouble with this query that fetches sorts forum topics on the number of replies in a different table. I tried this with Left join before the where but some data was left out in my while loop.
SELECT forum_topics.*, COUNT(forum_posts.comment_id) AS replies
FROM forum_topics
WHERE forum_topics.subcat_id = '$subcatid'
LEFT JOIN forum_posts
ON forum_topics.topic_id=forum_posts.topic_num
ORDER BY replies DESC
It gives me this as an error:
You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near 'LEFT JOIN forum_posts ON
forum_topics.topic_id=forum_posts.topic_num ORDER BY r' at line 1
This is the query that was working before:
SELECT * FROM forum_topics WHERE subcat_id = '$subcatid' ORDER BY date DESC
To echo I use:
$getChildCategory = mysql_query($query) or die(mysql_error());
$num = mysql_num_rows($get);
if($num == 0){ echo 'No Posts';}
else{
while ($row = mysql_fetch_assoc($get)){
When echoing I only get 1 result with the left join but with the old one I got 2 which is what I expected.
That's because the clauses are in the wrong order.
This is the correct syntax (EDITED per comments below):
SELECT `forum_topics`.*, COUNT(`forum_posts`.`comment_id`) AS `replies`
FROM `forum_topics`
LEFT JOIN `forum_posts`
ON `forum_topics`.`topic_id` = `forum_posts`.`topic_num`
WHERE `forum_topics`.`subcat_id` = '$subcatid'
GROUP BY `forum_posts`.`topic_num`
ORDER BY `replies` DESC
When you perform any sort of JOIN, you create a sort of "virtual table" that is an amalgamation of all tables involved. The where clause operates on this "virtual table", so if you think about it it only makes sense for the WHERE clause to go after this table has been created.

ORDER BY breaking mysql query

Someone on here helped put together this extremely useful query!
SELECT * FROM results r
INNER JOIN categories c on r.id = c.result_id
WHERE c.name in ('$purpose', '$prop_type', '$loan_type')
GROUP BY r.id
HAVING COUNT(c.c_id) = 3
LIMIT 50 OFFSET 0
Which is working great! However, when I try to order the results by using
SELECT * FROM results r
INNER JOIN categories c on r.id = c.result_id
WHERE c.name in ('$purpose', '$prop_type', '$loan_type')
ORDER BY r.usefulness DESC
GROUP BY r.id
HAVING COUNT(c.c_id) = 3
LIMIT 50 OFFSET 0
I get a syntax error. Am I missing something obvious here?
Thanks a lot!
there is an order for the parts of the query, and ORDER BY should be in the end (before the LIMIT and the OFFSET... try this:
SELECT * FROM results r
INNER JOIN categories c on r.id = c.result_id
WHERE c.name in ('$purpose', '$prop_type', '$loan_type')
GROUP BY r.id
HAVING COUNT(c.c_id) = 3
ORDER BY usefulness DESC
LIMIT 50 OFFSET 0
also make sure that you have spaces in the end of any row...
notice that in mysql that is a shortened version for LIMIT x OFFSET y - you can just write LIMIT y,x
ORDER BY has to come after the HAVING.

mysql syntax error on inner join statement

I want to get the 5 last rows from two tables.
Here is the mysql statement I execute:
"SELECT title, thread_id
FROM threads AS t1
JOIN ON comments AS c1 ON t1.user_id = c1.user_id
WHERE t1.user_id=".$userID
." OR c1.user_id=".$userID.
" ORDER BY thread_id DESC
LIMIT 0,5"
Here is the mistake:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ON comments AS c1 ON t1.user_id = c1.user_id WHERE t1.user_id=1 OR c1.' at line 3
What am I doing wrong?!?
You have a superfluous ON before comments.
There isnt anything like JOIN ON should be just JOIN or INNER JOIN, LEFT JOIN. something like this:
"SELECT title, thread_id
FROM threads AS t1
JOIN comments AS c1 ON t1.user_id = c1.user_id
WHERE t1.user_id=".$userID
." OR c1.user_id=".$userID.
" ORDER BY thread_id DESC
LIMIT 0,5"
Remove the extra ON in 3rd line. It will works.
"SELECT title, thread_id
FROM threads AS t1
JOIN comments AS c1 ON t1.user_id = c1.user_id
WHERE t1.user_id=".$userID
." OR c1.user_id=".$userID.
" ORDER BY thread_id DESC
LIMIT 0,5"

Categories