select a.nume_echipament,
a.producator,
a.seria,
b.uc,
b.port ,
a.durata_aprovizionare as durata_aprovizionare,
( SELECT sec_to_time(count(starea)*5) from echipamente inner join
aprovizionari on
echipamente.nume_echipament=aprovizionari.nume_echipament
where
(
echipamente.ora>aprovizionari.ora_aprov
and
echipamente.data>aprovizionari.data_aprov)
and starea='1'
and
echipamente.nume_echipament='automat_imbuteliere') as durata_functionare,
a.durata_viata as durata_viata,
( select data_aprov from aprovizionari where nume_echipament='automat_imbuteliere' order by date(data_aprov) desc, time(ora_aprov) desc limit 1) as data_aprov,
( select ora_aprov from aprovizionari where nume_echipament='automat_imbuteliere' order by date(data_aprov) desc, time(ora_aprov) desc limit 1) as ora_aprov,
(select sec_to_time(count(starea)*5)) as durata_totala
from date_tehnice a
inner join echipamente b on a.nume_echipament=b.nume_echipament
inner join aprovizionari c on c.nume_echipament=a.nume_echipament
where a.nume_echipament='automat_imbuteliere'
and
b.starea='1';
The above query work perfectly fine, but i also need to get the result from this query
select `starea` from echipamente where nume_echipament='automat_imbuteliere' order by data desc, ora desc limit 1 ;
As you can see in the first query it depends only on b.starea='1', whereas the second query needs to get the latest value of 'starea'. 'starea' has only 0 and 1 values. So, how do i combine this 2 queries into 1 query in order to get the last value value of 'starea'?
To get the second query to have its own row then UNION them like this with as many null columns as needed to match the number of columns in your first query.
UNION
select
(select `starea` from echipamente where nume_echipament='automat_imbuteliere' order by data desc, ora desc limit 1 )
, null
, null
, null
, null
, null
Related
I got this error, I hope you may help me. I want to show a certain item in a search.
SELECT p.id, p.property_rank, p.pic_numb, p.att_numb, p.confirm, p.finalized ,p.deleted, p.user_id, p.add_date, p.visit_time,p.visit_date,p.sent_numb, p.contact_numb, zip_name, zip_id, p.street, p.sp_featured, p.property_title, p.b_price_unit, p.b_price_si, p.b_price, p.b_price, p.street_no, p.field_54,
p.field_409,
( SELECT `listing_type`.`id`
FROM `res_rpl_listing_types` AS `listing_type`
WHERE `listing_type`.`id` = (
SELECT `listing`.`type`
FROM `res_rpl_listings` AS `listing`
WHERE `listing`.`id` = p.`listing`)
) AS `listing_type_id`,
p.listing, p.googlemap_ln, p.googlemap_lt, p.category, p.b_bedrooms, p.b_bathrooms, p.sp_openhouse, p.b_price_period, p.b_lot_area_unit, p.b_lot_area_si, p.b_lot_area, p.b_lot_area, p.b_living_area_unit, p.b_living_area_si, p.b_living_area, p.b_living_area, p.description, p.sp_hot, p.sp_forclosure
FROM res_rpl_properties AS p
WHERE 1 AND p.`type` = '0' AND p.`confirm` = '1' AND p.`finalized` = '1' AND p.`deleted` = '0' AND p.`category` IN(9,8,10)
ORDER BY p.add_date DESC
LIMIT 0 , 12
The error is telling you that your subquery (selected as listing_type_id) returns more than one row. To rephrase - it's returning more than one value for listing_type_id. You should limit the results from the subquery to just one.
You have two options:
OR select just the first row of subquery
(SELECT `listing_type`.`id`
FROM `res_rpl_listing_types` AS `listing_type`
WHERE `listing_type`.`id` = (
SELECT `listing`.`type`
FROM `res_rpl_listings` AS `listing`
WHERE `listing`.`id` = p.`listing`
LIMIT 1
)
LIMIT 1
) AS `listing_type_id`
OR use IN to allow multiple comparation
(SELECT `listing_type`.`id`
FROM `res_rpl_listing_types` AS `listing_type`
WHERE `listing_type`.`id` IN (
SELECT `listing`.`type`
FROM `res_rpl_listings` AS `listing`
WHERE `listing`.`id` = p.`listing`)
LIMIT 1
) AS `listing_type_id`
The problem is there is 2 subqueries and you need to treat both. Both of them need to be limited to 1 row only.
Im trying to get result with below query but its taking around 2min to retrieve
SELECT *
FROM customer e
WHERE e.id=324
AND e.g_id IN('x133fv','be6544','e992170','93611c')
and e.enrol_id =
(
select e1.enrol_id
from customer e1
WHERE e1.id=324
AND e1.g_id=e.g_id
ORDER BY update_time DESC, posted_time DESC, enrol_id DESC
LIMIT 1
)
I have index on (g_id,id)
Is there any other way to get the result via JOIN?
This is your query:
SELECT *
FROM customer e
WHERE e.id = 324 AND
e.g_id IN ('x133fv','be6544','e992170','93611c') and
e.enrol_id = (select e1.enrol_id
from customer e1
WHERE e1.id=324 AND e1.g_id=e.g_id
ORDER BY update_time DESC, posted_time DESC, enrol_id DESC
LIMIT 1
)
You can improve the performance with indexes. I would suggest: customer(id, g_id, update_time, posted_time, enrol_id).
I Have 2 tables, chamados(tickets) and atividades(activities) (1 for N)
Basically I need to select all 'chamados(tickets)' where last 'atividade(activity)' status is not 3 or 5;
I've tried within the SELECT and LEFT JOIN, nothing, I not got it yet.
My query is:
SELECT *
FROM (`chamados`)
ORDER BY `data_criacao_chamado` ASC
LIMIT 0,20;
I think that the query should seem something like:
SELECT *,
(
SELECT status FROM atividades WHERE atividade.fk_chamado = chamado.id_chamado ORDER BY id_atividade DESC LIMIT 1
) as status_item
FROM (`chamados`)
WHERE (status_item != 3 AND status_item != 5)
ORDER BY `data_criacao_chamado` ASC
LIMIT 0,20;
SELECT C.data_criacao_chamado FROM atividades A
JOIN
(SELECT fk_chamado, MAX(id_atividade) AS latest_atividade FROM atividades GROUP BY fk_chamado) LATEST_ATIVIDADES
ON A.fk_chamado = LATEST_ATIVIDADES.fk_chamado AND A.id_atividade = LATEST_ATIVIDADES.latest_atividade AND A.status_item NOT IN (3,5)
JOIN
chamados C ON C.id_chamado = A.fk_chamado
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;
Fellow coders, i have a table that contains a number of rows each with a date column. I would like to select the last 6 most recent rows. I can do that like this:
SELECT *
FROM `Stats`
WHERE `ProjectID` = ?
ORDER BY `StatsDate` DESC
LIMIT 6
This returns the rows I need but they are returned in DESC date order. What I want is the last 6 rows in ASC date order. How can I re-sort the output of the SELECT? Any ideas?
thanks
SELECT *
FROM (
SELECT *
FROM `Stats`
WHERE `ProjectID` = ?
ORDER BY `StatsDate` DESC
LIMIT 6
) s
ORDER BY s.StatsDate
Surround the query in an outer query and order that in a different order.
SELECT * FROM
(
SELECT *
FROM `Stats`
WHERE `ProjectID` = ?
ORDER BY `StatsDate` DESC
LIMIT 6
) s
ORDER BY `StatsDate` ASC
SELECT *
FROM (
FROM `Stats`
WHERE `ProjectID` = ?
ORDER BY `StatsDate` DESC
LIMIT 6
) as t
ORDER BY t.`StatsDate` ASC;