0

I want to get out of database 5 recently added articles by user and also count how many did he added in general. This was my query:

SELECT *
FROM articles
WHERE user = :user
ORDER BY publish_date DESC
LIMIT 5

What I simply changed to:

SELECT COUNT(*)
FROM (
    SELECT *
    FROM articles
    WHERE user = :user
    ORDER BY publish_date DESC
    LIMIT 5)

But this gives me an empty array. I don't want to split it into 2 queries, what can be the result?

jarlh
  • 42,561
  • 8
  • 45
  • 63
miloszowi
  • 36
  • 5
  • The second query lacks an alias name for the subquery at the end. Simply add some letter at the end to make it valid. The result you'll get will be 5 or less of course, because you limit the articles thus. – Thorsten Kettner Apr 12 '18 at 14:09
  • 2
    Add some sample table data and the expected result - as formatted text, not images. – jarlh Apr 12 '18 at 14:12
  • @ThorstenKettern Yea but when I do it, it just gives me back the count, I want to also get back an articles data – miloszowi Apr 12 '18 at 14:14
  • Then what shall your result look like? Either you can show five rows with the last articles or you can show one value for the total count. A result combining the two is hard to imagine. Please edit your request as jarlh suggested and show the expected result. – Thorsten Kettner Apr 12 '18 at 14:19

0 Answers0