-4

I have this query in mysql, some one know how to return an inverse query ?

mysql_query("
 SELECT * FROM batepapo 
 WHERE tipo='$t' && lang='$l' 
 ORDER BY id DESC LIMIT 0,100
");

The correct code now is

SELECT s.* FROM ( 
      SELECT t.* FROM batepapo
      t WHERE t.tipo='$t' AND t.lang='$l' 
      ORDER BY t.id DESC LIMIT 0,100) 
      AS s ORDER BY s.id")
8
  • 1
    What do you mean by inverse? The order of the results? What is returned?
    – oliakaoil
    Jul 11, 2014 at 16:42
  • What do you mean by inverse? The results that aren't in the first 100? Results from the equivalent table opapetab instead?
    – Mark Baker
    Jul 11, 2014 at 16:44
  • you mean ascending as oppose to descending?
    – codenathan
    Jul 11, 2014 at 16:44
  • mysql_query is an obsolete interface and should not be used in new applications and will be removed in future versions of PHP. A modern replacement like PDO is not hard to learn. If you're new to PHP, a guide like PHP The Right Way can help explain best practices. I hope you have used proper SQL escaping to avoid severe SQL injection bugs because this looks terrifyingly insecure.
    – tadman
    Jul 11, 2014 at 16:47
  • Eg: i have in the DB, Blue, red, gray, pink, Purple. i want return the last 3 rowns, but inverse, now it returns me purple, pink, gray but i want show gray, pink, purple Jul 11, 2014 at 16:49

3 Answers 3

3

One option is to use your existing query as an inline view query, and the outer query can reorder the results.

As an example:

SELECT s.*
  FROM ( SELECT t.* 
           FROM batepapo t
          WHERE t.tipo='fooval' 
            AND t.lang='langval' 
          ORDER BY t.id DESC
          LIMIT 0,100
       ) s
 ORDER BY s.id

using union syntax:

( SELECT t.* 
    FROM batepapo t
   WHERE t.tipo='fooval' 
     AND t.lang='langval' 
   ORDER BY t.id DESC
   LIMIT 0,100
) ORDER BY id

I'm obligated to add some recommendations. 1) Consider that your existing code may be subject to SQL Injection vulnerabilities (i.e. we don't see any calls to the mysql_real_escape_string function.) and 2) The mysql_ interface is deprecated, and new code should use either PDO or mysqli interface.

4
  • You salved my life thaks so much!!! it workeds fine to me i've change some thing i will post the changes Jul 11, 2014 at 17:12
  • there is a simpler way to write this.. use union syntax and you can avoid the additional query Jul 11, 2014 at 17:13
  • @JohnRuddell: You've piqued my curiosity. Firstly, how would the use of the UNION set operator achieve the specified resultset (note that we aren't given any guarantee that the rows are unique, nor are we guaranteed that the id column is the first column in the SELECT list). But beyond that, how would one use UNION set operator without using two SELECT keywords? Jul 11, 2014 at 17:18
  • @spencer7593 I just posted an answer showing what I meant.. doesn't require more than one SELECT keyword or even the UNION keyword :) Jul 11, 2014 at 17:25
0

mysql_query(" SELECT * FROM batepapo WHERE tipo='$t' && lang='$l' ORDER BY id ASC LIMIT 0,100 ");

2
  • All you have to do is remove DESC and it will default to ASC.
    – tadman
    Jul 11, 2014 at 16:48
  • but i wnat show the lest 100 rowns but reverse the show method Jul 11, 2014 at 16:51
0

just to clarify what I was saying on spencer7593's answer.. you can also write the query like this.

using union syntax:

(SELECT t.* 
 FROM batepapo t
 WHERE t.tipo='fooval' 
    AND t.lang='langval' 
 ORDER BY t.id DESC
 LIMIT 0,100) 
 ORDER BY id

basically you just do the select and then order afterwards. this seems like it wouldn't work and it isn't documented in MySQL, but its obvious from the grammar:

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.