0

I was thought how to make a complicated (as I think) SELECT query from table with 3 kind of comments (first - positive comments, second - negative comments and third - neutral comments). Using PHP I would like to SELECT and diplay first negative comments, and right after negative comments diplay all other type of comments. How to diplay them with one SELECT query together with LIMIT that I use to separate for pagination?

Example of table:

id - unique id

type - (value 1-positive, 2-negative, 3-neutral)

text - value

I was thought first SELECT * FROM comments WHERE type='2' ORDER BY id LIMIT 0,100

while(){

...

}

Right after that second

SELECT * FROM commetns WHERE type!='2' ORDER BY id LIMIT 0,100

while(){

...

}

But how use LIMIT for pagination if there is two different SELECT queries?

1
  • 2
    Wouldn't just ordering by type work for you? Commented Jun 26, 2015 at 11:35

5 Answers 5

2

Use a combination of UNION and LIMIT. However, you need to determine the bind variables, and specify the number of rows you want to display.

(SELECT * FROM comments WHERE type = '2' LIMIT ?)
UNION
(SELECT * FROM comments WHERE type != '2' LIMIT ?);

SQLFiddle: http://sqlfiddle.com/#!9/6d682/1

Sign up to request clarification or add additional context in comments.

Comments

1

Use an IF statement in the ORDER BY clause to change the type 2 to sort first:-

SELECT *
FROM comments
ORDER BY IF(type = 2, 0, type)
LIMIT 1, 20

This will give you all the negative (type 2) comments first, followed by the other comments (whether positive or neutral). You would probably want to add an extra column to the sort just for consistency of display.

5 Comments

what if I have already ORDER BY id DESC? Your variant works just as I need it. How to ORDER BY so that first go type=2 comments, then follow all other ORDER BY id DESC comments?
just have you order clause as ORDER BY IF(type = 2, 0, type), id DESC . This way they will be ordered by the type (negative first), and within type by descending id.
tried here sqlfiddle.com/#!2/b1b96/7 . It doesn't ORDER BY id DESC after type=2. On the example need id to go like: 4, 1, 3, 2. But with this ORDER BY usage it shows 4, 1, 2, 3
Ah, see what you want now. You want any type other than 2 to be ordered by id, rather than ordered by id within type. Try ORDER BY IF(type = 2, 0, 1), id DESC
perfect! now it works as I needed. great thanks to you. I'm so happy thanks to you =))
1

I didn't get your case exactly, but I think you may use OR operator to get what you want:

SELECT * from comments WHERE type=2 OR type=-2 ORDER BY type DESC

1 Comment

WHERE type in (2, -2) would be easier. not that it would matter for the DBMS, will result in the same.
0

you can use union to merge group of tables, but you must have the same columns in all the tables, for example:

select commetns,'nagtive' as type from nagtive_tbl limit 10
union
select  commetns,'positive' as type from positive_tbl limit 10
union
select  commetns,'neutral' as type from neutral_tbl limit 10

this query return table with all the comments from different tables each row contain is type so you can filter it while you building the lists on the client.

Comments

0

I must be missing context, otherwise you would just be fine using:

SELECT * from comments ORDER BY type ASC LIMIT 0,10

So by ordering the type, you'll first get all the items with type 1, followed by 2 and 3.

Just using the limit will chop them in the pieces you want and it will still respect the order.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.