Skip to content

Instantly share code, notes, and snippets.

@sajadshafizadeh
Created April 6, 2018 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sajadshafizadeh/1a492c0d6a1ae6afbca8bdd1ca10d659 to your computer and use it in GitHub Desktop.
Save sajadshafizadeh/1a492c0d6a1ae6afbca8bdd1ca10d659 to your computer and use it in GitHub Desktop.
SELECT e.id, e.table_code,
( SELECT id FROM events
WHERE author_id = 32
GROUP BY type, post_id, table_code, comment_id, context
ORDER BY date_time DESC
LIMIT 15 ) ids
FROM events e
WHERE e.author_id = 32 AND e.id >= MIN(ids)
ORDER BY date_time DESC
@mega6382
Copy link

mega6382 commented Apr 6, 2018

Try this:

SELECT e.id, e.table_code,

( SELECT id FROM events 
  WHERE author_id = 32
  GROUP BY type, post_id, table_code, comment_id, context
  ORDER BY date_time DESC
  LIMIT 15 ) ids

FROM events e
HAVING e.author_id = 32 AND e.id >= MIN(ids)
ORDER BY date_time DESC 

@sajadshafizadeh
Copy link
Author

#1054 - Unknown column 'e.author_id' in 'having clause'

@mega6382
Copy link

mega6382 commented Apr 6, 2018

Are you sure there is a column with the name author_id?

@sajadshafizadeh
Copy link
Author

sajadshafizadeh commented Apr 6, 2018

Yes I am.

@mega6382
Copy link

mega6382 commented Apr 6, 2018

OK, got it try this:


SELECT e.id, e.table_code,

( SELECT id FROM events 
  WHERE author_id = 32
  GROUP BY type, post_id, table_code, comment_id, context
  ORDER BY date_time DESC
  LIMIT 15 ) ids

FROM events e
WHERE e.author_id = 32
ORDER BY date_time DESC 
HAVING  e.id >= MIN(ids)

@sajadshafizadeh
Copy link
Author

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'HAVING e.id >= MIN(ids) LIMIT 0, 25' at line 12

@mega6382
Copy link

mega6382 commented Apr 6, 2018

Oh, sorry. Just move the last line before the ORDER BY

@mega6382
Copy link

mega6382 commented Apr 6, 2018

Now try this:

SELECT e.id, e.table_code,

FROM events e
WHERE e.author_id = 32 
AND e.id >= (SELECT MIN(id) FROM events 
  WHERE author_id = 32
  GROUP BY type, post_id, table_code, comment_id, context
  ORDER BY date_time DESC
  LIMIT 1)
ORDER BY date_time DESC 

@sajadshafizadeh
Copy link
Author

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FROM events e
WHERE e.author_id = 32
AND e.id >= (SELECT MIN(id) FROM events ' at line 3

@mega6382
Copy link

mega6382 commented Apr 6, 2018

I don't know what to do anymore. :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment