Return MYSQL QUERY INVERSED - php

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")

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.

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

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:

Related

Why Mysql order by doesn't work for this query

I have this query:
SELECT *
FROM (
SELECT p.id, product, unique_name, price, old_price, category_id, added_date
FROM products p, products_to_categories ptc, products_to_adverts pta
WHERE p.id=ptc.product_id AND (expire_date > now() OR expire_date=0)
AND p.id=pta.product_id AND p.active=1 AND p.instock=1 AND p.top_product="1"
and p.id not in (58,59,70,88,92,106,107,108,109)
and pta.advert_id not in (1,4,5,6,7,9,13,15,17)
ORDER BY added_date DESC
) as t GROUP BY id LIMIT 0,32
added_date field is datetime
Thanks !
You cannot use order by in subquery. Try using temporary table instead.
You can not use order by like this inside the query .As I think this is wrong.If you can do this using sql CASE
Follow this article.
[http://www.mysqltutorial.org/mysql-case-statement/][1]

sub queries order by

I am trying get records from question order by sub query (qcat) table. and my code is
"SELECT * FROM question
where survey_name='$_SESSION[ssn_sname]' AND
qcategory IN
( SELECT qcategory FROM qcat
WHERE client_name='$_SESSION[ssn_sname]'
GROUP BY qcategory
ORDER BY p_order
) AND
status='1' AND
survey_name LIKE'%$sname%
LIMIT $start, $limit";
But it did not get results in order.
how can i get rows order by the qcat table?
In the sub query you do not need the GROUP BY clause - This is used for aggregated functions
You also do not need the ORDER BY in the sub query - Let mysql work it out for the in bit
Add the ORDER BY for the whole query. i.e. at the end
So the SQL should be
"SELECT * FROM question
where survey_name='$_SESSION[ssn_sname]' AND
qcategory IN
( SELECT qcategory FROM qcat
WHERE client_name='$_SESSION[ssn_sname]'
) AND
status='1' AND
survey_name LIKE'%$sname%
ORDER BY p_order
LIMIT $start, $limit";
I also think that you may be able to avoid the sub query in the first place. But that would require a little thinking and a bit more knowledge about the tables.
BTW - Note the possibility of SQL injection
You need to join with the qcat table in order to be able to sort on a different column within that table. Try this:
$query = "
SELECT q.id question_id, q.*, c.*
FROM question q
INNER JOIN qcat c ON c.category = q.category
WHERE q.survey_name='$_SESSION[ssn_sname]'
AND c.client_name = '$_SESSION[ssn_sname]'
AND q.status='1'
AND q.survey_name like '%$sname%'
GROUP BY q.id
ORDER BY c.p_order
LIMIT $start, $limit";
Note: Your query is vulnerable to SQL Injection!

Not In mysql subquery Limit

Does anybody have any ideas how I can get around a #1235 - This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery' error?
My query is below ( I've read that I can upgrade mysql but this isn't possible):
$query = #mysql_query("SELECT * FROM posts
WHERE postid NOT IN
( SELECT postid FROM log
ORDER BY posted DESC
LIMIT 10)
ORDER BY (RAND() * Multiplier)
LIMIT 1");
According to this bug, you can use this ugly workaround:
SELECT * FROM t1 WHERE s1 NOT IN
(SELECT * FROM (SELECT s2 FROM t2 ORDER BY s1 LIMIT 1) AS alias)
You can rewrite your query using JOIN:
SELECT *
FROM posts NATURAL LEFT JOIN (
SELECT postid FROM log ORDER BY posted DESC LIMIT 10
) t
WHERE t.postid IS NULL
ORDER BY RAND()
LIMIT 1
Be aware, however, that ORDER BY RAND() is very expensive. Not only must a random value be calculated for each record, but then a sort must be performed on the results. Indexes are of no use.
You would fare better if you had a column col containing unique integers, then with an index on col you can very rapidly obtain a random record with:
SELECT *
FROM posts NATURAL LEFT JOIN (
SELECT postid FROM log ORDER BY posted DESC LIMIT 10
) t JOIN (
SELECT RAND() * MAX(col) AS rand FROM posts
) r ON posts.col >= r.rand
WHERE t.postid IS NULL
LIMIT 1
Note that the uniformity of such "randomness" will depend on the distribution of the integers within col after any other filtering has taken place.

MySQL Query with DISTINCT and ORDER BY

Here is my query:
$result = $mysqli->query('SELECT DISTINCT SKU_SIZE_PART1
FROM SKU_DATA
ORDER BY SKU_SIZE_PART1 DESC');
Now this works perfect for SKU_SIZE_PART1 but I have 2 more parts that I need to grab. Now when I put a comma and do this: 'SKU_SIZE_PART1, SKU_SIZE_PART2, SKU_SIZE_PART3' then the DISTINCT doesn't work and I get a ton of duplicates, and then I'm not sure how to order the query so that all of them are ordered by the size and DESC.
Does that make sense? I could just duplicate that query 2 more times and have 3 separate queries but I would like to know how to accomplish this with just one.
I'm not positive that I understand what you're trying to do, but it sounds like you might actually want something like this:
SELECT SKU_SIZE_PART1 AS SKU_SIZE_PART
FROM SKU_DATA
UNION
SELECT SKU_SIZE_PART2 AS SKU_SIZE_PART
FROM SKU_DATA
UNION
SELECT SKU_SIZE_PART3 AS SKU_SIZE_PART
FROM SKU_DATA
ORDER BY SKU_SIZE_PART DESC
which will return all distinct SKU_SIZE_PART1/2/3 values in a single column, rather than all distinct (SKU_SIZE_PART1, SKU_SIZE_PART2, SKU_SIZE_PART3) triads in three columns.
After reading your question several times, I figured this might be what you are looking for:
SELECT SKU_SIZE_PART1 AS ssp
FROM SKU_DATA
UNION
SELECT SKU_SIZE_PART2 AS ssp
FROM SKU_DATA
UNION
SELECT SKU_SIZE_PART3 AS ssp
FROM SKU_DATA
ORDER BY ssp DESC
SELECT d.sku_size_part1, d.sku_size_part2, d.sku_size_part3
FROM sku_data d
WHERE d.id IN (
SELECT s.id <<--- replace `id` with the real primary-key for table `sku_data`
FROM sku_data s
GROUP BY s.sku_size_part1)
ORDER BY d.sku_size_part1 DESC
Note that this will select rows more or less at random.
Although all sku_size_parts will be from the same row, lots of values will be hidden.
If you want to make the query stable, you need to add a having clause in the inner subselect.
Something like this:
SELECT d.sku_size_part1, d.sku_size_part2, d.sku_size_part3
FROM sku_data d
WHERE d.id IN (
SELECT s.id <<--- replace `id` with the real primary-key for table `sku_data`
FROM sku_data s
GROUP BY s.sku_size_part1
HAVING s.sku_size_part2 = MIN(s.sku_size_part2)
AND s.sku_size_part3 = MIN(s.sku_size_part3))
ORDER BY d.sku_size_part1 DESC
Either that or you want #bfavaretto's UNION variant.
DISTINCT selects a distinct set of rows, not columns... the assumption/problem here is how to condense multiple columns. If you had the following table
sku1 | sku2 | sku3
---------------------
a | a | b
b | b | b
Telling it to select destinct would return both rows because none of them are distinct, you couldn't just remove the third column because then the row data would be inconsistent. If you want everything in one table you can do this with subqueries.
SELECT (SELECT DISTINCT SKU_SIZE_PART1 FROM SKU_DATA ORDER BY SKU_SIZE_PART1 DESC)
as part1, (SELECT DISTINCT SKU_SIZE_PART2 FROM SKU_DATA ORDER BY SKU_SIZE_PART2 DESC)
as part2, (SELECT DISTINCT SKU_SIZE_PART3 FROM SKU_DATA ORDER BY SKU_SIZE_PART1 DESC)
as part3 FROM SKU_DATA
You can read up a little on how DISTINCT works to see why you can't just do SELECT DISTINCT SKU_SIZE_PART1, PART2, PART3. Somewhere like This Link

Different ORDER BY for each SELECT in a UNION with MySQL

Using PHP and MySQL, is there a way to use a different ORDER BY for each of the SELECT statements in a UNION?
SELECT * FROM the_table WHERE color = 'blue' ORDER BY price ASC LIMIT 5
UNION ALL
SELECT * FROM the_table WHERE color = 'red' ORDER BY RAND() LIMIT 10
The above statement does not work. It seems you can only do an ORDER BY on the final result set. Is there a way to do an ORDER BY on the first SELECT then a different ORDER BY on the second SELECT using UNION?
(SELECT * FROM the_table WHERE color = 'blue' ORDER BY price ASC LIMIT 5)
UNION ALL
(SELECT * FROM the_table WHERE color = 'red' ORDER BY RAND() LIMIT 10)
Please note that this does not work if you don't specify a LIMIT (though you can specify a very large dummy limit). See mysql documentation (13.2.7.3. UNION Syntax):
"Use of ORDER BY for individual SELECT statements implies nothing about the order in which the rows appear in the final result because UNION by default produces an unordered set of rows...
"To cause rows in a UNION result to consist of the sets of rows retrieved by each SELECT one after the other, select an additional column in each SELECT to use as a sort column and add an ORDER BY following the last SELECT:
"(SELECT 1 AS sort_col, col1a, col1b, ... FROM t1)
UNION
(SELECT 2, col2a, col2b, ... FROM t2) ORDER BY sort_col;
To additionally maintain sort order within individual SELECT results, add a secondary column to the ORDER BY clause:
"(SELECT 1 AS sort_col, col1a, col1b, ... FROM t1)
UNION
(SELECT 2, col2a, col2b, ... FROM t2) ORDER BY sort_col, col1a;"

Categories