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"
Related
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
$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.
I am building a custom forum in CodeIgniter. I have four primary tables: parents(categories), children(boards), threads, and messages(replies). The thread table only contains the thread id, author, title, date, and the id of the first message. When a thread is viewed, it takes the column "first_msg_id" to retrieve the content of the thread.
I would like to create a query that gets the count of all the replies in a specific board. Basically, any message that doesn't match a thread's first_msg_id field. Here is what I have so far:
$query = "
SELECT
m.message_id, m.thread_id
t.thread_id, t.first_msg_id
FROM forum_messages AS m
LEFT JOIN forum_threads AS t ON t.first_msg_id != m.message_id
WHERE t.child_id = ".$board_id."
ORDER BY m.message_id";
This is the error I'm getting:
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 '.thread_id, t.first_msg_id FROM forum_messages AS m ' at line 3
Here are my tables:
Update
Ok, so now that I got my query working(well, fixed the error), it's not doing what I want... I think I have my logic wrong. I wrote a script that created 40 new threads in one board. None of them have replies. When I use mysql num rows with the query I made, I get a result of 1560. It should return 0. Why is this happening??? Lol.
Got it working.
You are missing a comma after m.thread_id
Something like
SELECT
m.message_id, m.thread_id,
t.thread_id, t.first_msg_id
FROM forum_messages AS m
LEFT JOIN forum_threads AS t ON t.first_msg_id != m.message_id
WHERE t.child_id = ".$board_id."
ORDER BY m.message_id
Comma missing , after m.thread_id
Try with -
$query = "SELECT m.message_id, m.thread_id, t.thread_id, t.first_msg_id ....
More appropriate -
$query = "
SELECT
m.message_id, m.thread_id,
^
-------------------------|
t.thread_id, t.first_msg_id
FROM forum_messages AS m
LEFT JOIN forum_threads AS t ON t.first_msg_id != m.message_id
WHERE t.child_id = ".$board_id."
ORDER BY m.message_id
";
I've build the following query:
(SELECT
privatemsgs.id,
privatemsgs.useradn,
privatemsgs.useraid,
privatemsgs.title,
privatemsgs.created,
privatemsgs.timee,
privatemsgs.isread,
u.photo AS creatorphoto,
privatemsgs.relatedto
FROM privatemsgs
LEFT JOIN
users AS u ON(privatemsgs.useraid = u.id)
WHERE userbid='5'
AND relatedto=0 and bdel=1)
UNION ALL
(SELECT
privatemsgs.id,
privatemsgs.useradn,
privatemsgs.useraid,
privatemsgs.title,
privatemsgs.created,
privatemsgs.timee,
privatemsgs.isread,
u.photo AS creatorphoto,
rel.relatedto
FROM privatemsgs AS rel
JOIN privatemsgs ON(rel.relatedto = privatemsgs.id)
LEFT JOIN
users AS u ON(rel.useraid = u.id)
WHERE rel.userbid='5')
GROUP BY id
ORDER BY timee DESC
This query select all Privatemsgs from the tables, and acting like mail,FOR EX:
If I sent a msg to user b, and user b answered me. I want to display the msg in inbox and outbox of each user.
A comment to private msg marked as "relatedto" the id of the main msg.
The query works, but duplicate the msgs in display (same msg display many times)
I tried to do "GROUP BY id" in order to fix it but i got the error:
#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 'GROUP BY id ORDER BY timee DESC'
THANK YOU!!
First, as per PM 77's comment, a union instead of a union all will solve your problem of duplicates. You don't need a group by clause at all.
Second, you might have a logic error. The top have of your union query has this:
FROM privatemsgs
LEFT JOIN
users AS u ON(privatemsgs.useraid = u.id)
WHERE userbid='5'
AND relatedto=0 and bdel=1)
If any of those fields in the where clause are in the users table, your left join has become an inner join. To keep it as a left join, you have to put all the filters in the join, like this:
FROM privatemsgs
LEFT JOIN
users AS u ON privatemsgs.useraid = u.id
AND userbid='5'
AND relatedto=0 and bdel=1)
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.