Querying multiple database tables without using JOIN? - mysql

I am attempting to combine three queries into one and failing miserably. Everything goes well until I add the "order by" clause.
SELECT
wti.price, hh.price, mb.price
FROM
spotprices.wti, spotprices.hh, spotprices.mb
ORDER BY
wti.sdate desc limit 1, hh.sdate desc limit 1, mb.sdate desc limit 1;
In essence, I am attempting to get the most recent price by sdate by using the "order by sdate desc limit 1" trick. Individually, the query works well.
SELECT price FROM spotprices.wti ORDER BY wti.sdate desc LIMIT 1;

Use subqueries in the select:
SELECT (SELECT wti.price FROM spotprices.wti ORDER BY wti.sdate DESC LIMIT 1) as wti_price,
(SELECT hh.price FROM spotprices.hh ORDER BY hh.sdate DESC LIMIT 1) as hh_price,
(SELECT mb.price FROM spotprices.mb ORDER BY mb.sdate DESC LIMIT 1) as mb_price;

Related

MySQL LIMIT inside a subquery [duplicate]

this is the code what im using
$Last_Video = $db->fetch_all('
SELECT VID, thumb
FROM video
WHERE VID IN (
SELECT VID
FROM video
WHERE title LIKE "%'.$Channel['name'].'%"
ORDER BY viewtime DESC
LIMIT 5)
ORDER BY RAND()
LIMIT 1
');
This is the error what give me
Message: Error during SQL execution: SELECT VID, thumb FROM video WHERE VID IN ( SELECT VID FROM video WHERE title LIKE "%funny%" ORDER BY viewtime DESC LIMIT 5) ORDER BY RAND() LIMIT 1<br />
MySQL Error: This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'<br />
MySQL Errno: 1235
how i can fix this problem ? its other way to make it ... so i dont get the error ...
Instead of using IN, you can use JOIN
SELECT v.VID, v.thumb
FROM video AS v
INNER JOIN
(SELECT VID
FROM video
WHERE title LIKE "%'.$Channel['name'].'%"
ORDER BY viewtime DESC
LIMIT 5) as v2
ON v.VID = v2.VID
ORDER BY RAND()
LIMIT 1
You can use below to bypass this error.
$Last_Video = $db->fetch_all('
SELECT VID, thumb
FROM video
WHERE VID IN (select * from (
SELECT VID
FROM video
WHERE title LIKE "%'.$Channel['name'].'%"
ORDER BY viewtime DESC
LIMIT 5) temp_tab)
ORDER BY RAND()
LIMIT 1
');
You don't need a subquery here. Try this:
SELECT VID, thumb
FROM video
WHERE title LIKE "%'.$Channel['name'].'%"
ORDER BY RAND() DESC
LIMIT 1
In MySQL 5.0.26 and later, you will get an error:
MySQL does not support LIMIT in subqueries for certain subquery operators:
Reference.
add this is your in condition
(SELECT * FROM (
SELECT * FROM table ORDER BY id DESC LIMIT 50
) sub
ORDER BY id ASC)
Why you cant use simple: ?
SELECT v.VID, v.thumb
FROM video as v
WHERE title LIKE "%'.$Channel['name'].'%"
ORDER BY viewtime DESC
LIMIT 5
what for subqueries here?
mysql is disabled read it ORACLE
DELETE FROM wall_orders WHERE order_id IN (
SELECT order_id FROM (SELECT order_id, COUNT(orders_products_id) as cnt FROM wall_orders_products GROUP BY order_id ORDER BY cnt DESC LIMIT 1000) y1 WHERE cnt > 170 LIMIT 1000)
235 - This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
Simple SQL and not possible

Count variable with limit in MySQL?

Nobody can answer this question?
$result=mysql_query("
SELECT COUNT(*) AS `total` FROM `mytable`
WHERE `myvariable`='1'
ORDER BY `id` DESC
LIMIT 15;"
);
$data=mysql_fetch_array($result);
$count = $data['total'];
echo $count;
This count ALL result from mytable, but how I can do to count last 15 results only? It seems LIMIT 15 not work in this case?
I think this is the query you want:
SELECT SUM(myvariable = '1') AS total
FROM (SELECT myvariable
FROM mytable
ORDER BY id DESC
LIMIT 15) AS subquery
This only looks at the most recent 15 rows, and counts the number of them that have myvariable = 1.
Since you want last 15 after descending order, Order by ascending and select first 15 and do descending order sort
select * from (SELECT * FROM mytable WHERE myvariable='1' ORDER BY id ASC LIMIT 15) ORDER BY id DESC

SQL: UNION realestate priorities offers

i'm trying to write a sql query to list all offers from database but first to retrive priorities NOT NULL order by rand then priorities NULL order by id.
I`ve made some kind of this:
(SELECT anunt_lista_id
FROM anunturi__lista
WHERE anunt_lista_is_prioritar IS NOT NULL
ORDER BY RAND())
UNION
(SELECT anunt_lista_id
FROM anunturi__lista
WHERE anunt_lista_is_prioritar IS NULL
ORDER BY anunt_lista_id ASC)
LIMIT 100
but ORDER BY is ignored, no one is applied.
There is another way to this and using another way to do RAND() because as i know this is too slow?
ORDER BY in individual parts of UNION is only useful when you add a LIMIT clause to that specific part, otherwise it is ignored.
From the docs:
To apply ORDER BY or LIMIT to an individual SELECT, place the clause
inside the parentheses that enclose the SELECT:
(SELECT a FROM t1 WHERE a=10 AND B=1 ORDER BY a LIMIT 10)
UNION
(SELECT a FROM t2 WHERE a=11 AND B=2 ORDER BY a LIMIT 10);
However, 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. Therefore,
the use of ORDER BY in this context is typically in conjunction with
LIMIT, so that it is used to determine the subset of the selected rows
to retrieve for the SELECT, even though it does not necessarily affect
the order of those rows in the final UNION result. If ORDER BY appears
without LIMIT in a SELECT, it is optimized away because it will have
no effect anyway.
If you want the first part of the results to be sorted first, you have to workaround it in some way:
(SELECT anunt_lista_id
FROM anunturi__lista
WHERE anunt_lista_is_prioritar IS NOT NULL)
UNION
(SELECT anunt_lista_id
FROM anunturi__lista
WHERE anunt_lista_is_prioritar IS NULL)
ORDER BY (anunt_lista_is_prioritar IS NULL) ASC,
CASE WHEN anunt_lista_is_prioritar IS NOT NULL THEN
RAND()
ELSE anunt_lista_id END
LIMIT 100
The first clause of the order by: (anunt_lista_is_prioritar IS NULL) ASC is going to give false (0) when it isn't NULL and true (1) when it is NULL. Since it is ordering ASC, 0 will appear first than 1.
You can make dummy columns for order by!
Try this:
SELECT
if(anunt_lista_is_prioritar IS NOT NULL, rand() , 0) as fld_1,
if(anunt_lista_is_prioritar IS NULL,anunt_lista_id , 0) as fld_2,
anunt_lista_id
FROM anunturi__lista
order by fld_1 desc , fld_2 asc
Limit 100

Is it possible to order "first n" records randomly with only one SELECT?

Is there a way to do this without two selects ?
Original query
SELECT name,view_count
FROM `ex`.`item`
where status='available' order by view_count asc
limit 40;
To display randomly
SELECT *
FROM (SELECT name,view_count
FROM `ex`.`item`
where status='available'
order by view_count asc
limit 40 ) AS temp
ORDER BY RAND();
Can this be done without a second select?
Try this:
SQL Fiddle: http://sqlfiddle.com/#!2/330f8/2
SELECT name, view_count
FROM `ex`.`item`
where status='available'
order by rand(), view_count asc limit 40;

MySQL Sorting using order by not working using unio

I'm using an union statement in mysql but i've some problems sorting the results. The ORDER statement doesn't works at all, the results comes out always sorted by the id field.
Here an example query:
SELECT a.* FROM ( ( select * from ticket_ticket AS t1 WHERE ticket_active=1 ORDER BY t1.ticket_date_last_modified DESC )
UNION ( select * from ticket_ticket AS t2 WHERE ticket_active=0 ORDER BY t2.ticket_date_last_modified DESC, t2.ticket_status_id DESC ) )
AS a LIMIT 0,20;
I want to order the results of the first SELECT by last_modified time, and the second SELECT by time and status. But the ORDER statement get just skipped. The results always come out ordered by the ticket_id ( the PRIMARY KEY ).
What's wrong in this query ?
Thanks!
Ok, i've fixed it writing the query this way:
SELECT a.*
FROM
(SELECT *
FROM ticket_ticket
WHERE ticket_active=1
ORDER BY ticket_date_last_modified DESC) AS a
UNION ALL
SELECT b.*
FROM
(SELECT *
FROM ticket_ticket
WHERE ticket_active=0
ORDER BY ticket_date_last_modified DESC, ticket_status_id DESC) AS b LIMIT 0,
20;
You are using a UNION query that will return distinct values, and the order of the returned rows is not guaranteed.
But you don't need an union query for this:
select *
from ticket_ticket AS t1
ORDER BY
ticket_active!=1,
ticket_date_last_modified DESC,
ticket_status_id DESC
LIMIT 0,20;