Having problem with sql query in Opencart database - mysql

I'm trying to update 1 field from another table's data but I'm getting the error #1242 - Subquery returns more than 1 row.
The query I'm running is:
UPDATE oc_order AS o
SET o.date_added = ( SELECT date_added FROM oc_order_history
WHERE order_id = o.order_id
AND order_status_id = 5)
Any ideas on where it's going wrong or how I can fix?

You should use JOIN.
UPDATE oc_order oc
INNER JOIN
(
SELECT date_added ,order_id
FROM oc_order_history
WHERE order_status_id = 5
) as oh on oc.order_id=oh.order_id
set oc.date_added=oh.date_added ;
I supposed that order_id is the key for JOIN condition.
Test it and let me know if it helps.
It would be better if you gave some examples data to get a right answer.

Related

select last record in sub query

have read all similar qs but cant apply to my sql
Would like to select all customers AND the last action record inserted.
The sql below first selects the max actionid then uses that on another sub query - this takes 5+secs to run ;(
Please advise TQ
SELECT cus.cusid,cus.FirstName,cus.Surname,
lastact.actionid, lastact.actiondate, lastact.siteid
FROM cus
LEFT JOIN(
SELECT MAX(actionid) AS maxactionid, cusid
FROM `action`
INNER JOIN `event` ON event.eventid = action.`eventid`
GROUP BY cusid
) AS maxactionid ON maxactionid.cusid = cus.cusid
LEFT JOIN (
SELECT
action.actionid,
action.actiondate,
event.cusid,
event.siteid
FROM
`action`
INNER JOIN `event`
ON event.eventid = action.eventid
ORDER BY actionid DESC
) AS lastact ON lastact.actionid = maxactionid
WHERE UCASE(CONCAT(firstname, surname)) LIKE '%JIM%HEMM%'
TQ for ideas - please see following:
1) the limit idea, provides null results for lastact.actionid, lastact.actiondate, lastact.siteid - but does run in 0.075 secs!
Such a shame this idea fails
SELECT cus.cusid,cus.FirstName,cus.Surname, lastact.actionid, lastact.actiondate, lastact.siteid
FROM cus
LEFT JOIN (SELECT action.actionid, action.actiondate, event.cusid, event.siteid
FROM action
INNER JOIN event ON event.eventid = action.eventid
ORDER BY actionid DESC LIMIT 1
) AS lastact ON lastact.cusid = cus.cusid
WHERE UCASE(CONCAT(firstname, surname)) LIKE '%JIM%HEMM%'
2) EXPLAIN results of original query are:
3) Adding LIKE 'JIM%' AND cus.surname LIKE 'HEMM%' doesn't affect query time much but will include as per suggestion
Hi - have got great result by using ideas from everyone - Thank you
1) Changed WHERE to cus.FirstName LIKE 'JIM%' AND cus.surname LIKE 'H%'
2) Added index on firstname, surname
3) Added cusid in action table (don't need event table anymore)
4) Moved lookup tables (not in orig question) outside of action sub query
Finished sql looks like (runs in 0.063 secs - tested with a surname of only one letter!)
SELECT cus.cusid,cus.FirstName,cus.Surname, lastact.actionid, lastact.actiondate, lastact.siteid, actiontype.action,
FROM cus
LEFT JOIN (
SELECT action.actionid, action.actiondate, event.cusid, event.siteid
FROM action
ORDER BY actionid DESC
) AS lastact ON lastact.cusid = cus.cusid
LEFT JOIN actiontype ON actiontype.actiontypeid = lastact.typeid
WHERE cus.FirstName LIKE 'JIM%' AND cus.surname LIKE 'H%'
GROUP BY lastact.cusid
As JC Sama said "change select MAX(actionid) by select actionid and adding a limit 1 and order by desc", helps indexed
searchs.
As David K-J said "run an EXPLAIN first to see what the planner is trying to do. I would suspect it's the (non-indexable) search on concatenation strings".
You shouldn't put jokers '%' at the begining of a string when comparing, that disables indexed search.
You shouldn't use functions when comparing (at least, avoid them if you can), also for the indexed search.
Now that you can use indexes, add them if you haven't done it yet.
I may be wrong here, but I don't see the point of the last LEFT JOIN, as far as I'm concerned. You could withdraw that data from the first LEFT JOIN. Neither why are you grouping by cusid.
With all, the sql I made is (obviously not tested, you may have to fix some thing):
SELECT cus.cusid,cus.FirstName,cus.Surname,
maxaction.actionid, maxaction.actiondate, maxaction.siteid
FROM cus
LEFT JOIN(
SELECT actionid AS maxaction, action.actiondate, event.cusid, event.siteid
FROM `action`
INNER JOIN `event` ON event.eventid = action.eventid
order by actionid desc limit 1
) AS maxaction ON maxaction.cusid = cus.cusid
WHERE cus.FirstName like 'JIM%' and cus.surname like 'HEMM%'

Mysql: WHERE not working in subquery?

I'm trying to get the WHERE part of my subquery to work below. I can see that 'where event_id=..' is ambiguous because the parent query is looking at the same table.
Is it even possible to have a WHERE in a same-table subquery?
UPDATE tickets SET tickets.ticket_number = (
SELECT max_ticket
FROM (
SELECT (MAX(ticket_number)+1) AS max_ticket
FROM tickets
WHERE event_id=10045
)
AS sub_table
)
WHERE ticket_id=68
Any help really appreciated.
Possibly try it as a join
UPDATE tickets a
INNER JOIN
(
SELECT (MAX(ticket_number)+1) AS max_ticket
FROM tickets
WHERE event_id = 10045
) b
SET a.ticket_number = b.max_ticket
WHERE a.ticket_id = 68

How can I pull out only records that have FAIL against them?

I'm sure this will be quite simple for some one clued up in SQL but I think it needs a sub query or something. I have a table which basically has a load of order numbers in it and a reply column from an XML API. Either FAIL or SUCCESS.
A brand new row is inserted into the DB after every request. So there may be 5 FAILS for one order number, and on the 6th attempt a record is inserted saying SUCCESS.
How can I put out order numbers that ONLY have a FAIL status next to them?
This will allow me to figure out what records need looking into that continuously fail in the API request.
Try this, by grouping your orders with primary key (order_id)
SELECT * FROM
(
SELECT GROUP_CONCAT(status) as status_combined, order_id
FROM orders
GROUP BY order_id
) AS order_tmp
WHERE status_combined NOT LIKE '%SUCCESS%'
Edit (As per asker comments)
SELECT * FROM
(
SELECT GROUP_CONCAT(status) as status_combined, order_id
FROM orders
JOIN certificates ON certificates.Ordernumber = orders.OrderNumber
GROUP BY order_id
) AS order_tmp
WHERE status_combined NOT LIKE '%SUCCESS%'
please make sure you need to join based on "Ordernumber" or "order_id"
Try this
select m.*
from Main m
join Transactiontable tt
on m.orderid = tt.orderid
group by tt.status , m.orderid
having count(case when tt.status = "failed") = count(tt.status)
You can use simple sql query using a where clause:
select *
from some_table
where Column_From_some_table_has_value = your_particular_value
thats enough.
You can have a look at How to use where clause in sql
Thanks
This is probably the cleanest way to do it:
select *
from mytable
where id in (
select id
from mytable
group by id
having sum(status = 'SUCCESS') = 0)
I'm not a fan of #Minesh's answer because it uses both an aggregate function and the LIKE operator. Both of those can cause performance issues since there won't be any indexes to help the query out with the difficult part of the work. The LIKE clause particularly is a lot of work for the database since it will need to scan every result.
I'm more familiar with SQL Server, but this should work well for you:
SELECT *
FROM Orders
WHERE OrderNumber NOT IN (
SELECT OrderNumber
FROM Orders
WHERE Status = 'SUCCESS')
AND OrderNumber NOT IN (
SELECT OrderNumber
FROM Certificates
WHERE OrderStatus = 'CANCELLED')

MySQL update value from the same table with count

What I want to do is to set every patient its unique patient code which starts with 1 and it's not based on row id. Id only specifies order. Something like this:
patient_id patient_code
2 1
3 2
4 3
This is my query:
UPDATE patients p1
SET p1.patient_code = (
SELECT COUNT( * )
FROM patients p2
WHERE p2.patient_id <= p1.patient_id
)
But it is throwing error:
#1093 - You can't specify target table 'p1' for update in FROM clause
I found this thread: Mysql error 1093 - Can't specify target table for update in FROM clause.But I don't know how to apply approved answer this to work with subquery WHERE which is necessary for COUNT.
UPDATE
patients AS p
JOIN
( SELECT
p1.patient_id
, COUNT(*) AS cnt
FROM
patients AS p1
JOIN
patients AS p2
ON p2.patient_id <= p1.patient_id
GROUP BY
p1.patient_id
) AS g
ON g.patient_id = p.patient_id
SET
p.patient_code = g.cnt ;
I found working solution, but this is just workaround:
SET #code=0;
UPDATE patients SET patient_code = (SELECT #code:=#code+1 AS code)
Try this,
UPDATE patients p1 INNER JOIN
(
SELECT COUNT(*) as count,patient_id
FROM patients
group by patient_id
)p2
SET p1.patient_code=p2.count
WHERE p2.patient_id <= p1.patient_id
SQL_LIVE_DEMO
Thanks to Mari's answer I found a solution to my similar problem. But I wanted to add a bit of an explanation which for me at first wasn't too clear from his answer.
What I wanted to do would have been as simple as the following:
UPDATE my_comments AS c
SET c.comment_responses = (
SELECT COUNT(c1.*) FROM my_comments AS c1
WHERE c.uid = c.parent_uid
);
Thanks to Mari I then found the solution on how to achieve this without running into the error You can't specify target table 'p1' for update in FROM clause:
UPDATE my_comments AS c
INNER JOIN (
SELECT c1.parent_uid, COUNT(*) AS cnt
FROM my_comments AS c1
WHERE c1.parent_uid <> 0
GROUP BY c1.parent_uid
) AS c2
SET c.comment_responses = c2.cnt
WHERE c2.parent_uid = c.uid;
My problems before getting to this solution were 2:
the parent_uid field doesn't always contain an id of a parent which is why I added the WHERE statement in the inner join
I didn't quite understand why I would need the GROUP BY until I executed the SELECT statement on it's own and the answer is: because COUNT groups the result and really counts everything. In order to prevent this behavior the GROUP BY is needed. In my case I didn't have to group it by uid though but the parent_uid to get the correct count. If I grouped it by uid the COUNT would always be 1 but the parent_uid existed multiple times in the result. I suggest you check the SELECT statement on it's own to check if it's the result you expect before you execute the full UPDATE statement.

Mysql join query - phpmyadmin error is 'not unique table alias'

Guys was looking for a little help. I was trying to run this query in php myadmin. Needing to update a temp table with 4 sets of data..3 sets from one table..and 1 from another table, while joining 3 tables. The error comes back after my second JOIN that I do not have a unique table alias. I'm not sure how to fix that. Can anyone see where the query is going wrong? Thanks.
INSERT INTO jos_jxgallery_assign_ratings_temp( image_id, pscore, hits, contest_id )
SELECT jos_jxgallery_images.image_id, jos_jxgallery_images.pscore, jos_jxgallery_images.hits, jos_jxgallery_contests.contest_id
FROM jos_jxgallery_images, jos_jxgallery_contests
INNER JOIN jos_jxgallery_contest_image_map ON jos_jxgallery_images.image_id = jos_jxgallery_contest_image_map.image_id
INNER JOIN jos_jxgallery_contests ON jos_jxgallery_contest_image_map.contest_id = jos_jxgallery_contests.contest_id
WHERE jos_jxgallery_contests.published =1
AND jos_jxgallery_images.published =1
ORDER BY jos_jxgallery_images.pscore DESC , jos_jxgallery_images.hits DESC
LIMIT 25
MySQL said: Documentation
#1066 - Not unique table/alias: 'jos_jxgallery_contests'
Now when I change the query to this...I get another error listed below the query. Any ideas?
INSERT INTO jos_jxgallery_assign_ratings_temp( image_id, pscore, hits, contest_id )
SELECT jos_jxgallery_images.image_id, jos_jxgallery_images.pscore, jos_jxgallery_images.hits, jos_jxgallery_contests.contest_id
FROM jos_jxgallery_images, jos_jxgallery_contests
INNER JOIN jos_jxgallery_contest_image_map ON jos_jxgallery_images.image_id = jos_jxgallery_contest_image_map.image_id
INNER JOIN jos_jxgallery_contests AS con ON jos_jxgallery_contest_image_map.contest_id = con.contest_id
WHERE con.published =1
AND jos_jxgallery_images.published =1
ORDER BY jos_jxgallery_images.pscore DESC , jos_jxgallery_images.hits DESC
LIMIT 25
MySQL said: Documentation
#1054 - Unknown column 'jos_jxgallery_images.image_id' in 'on clause'
You use the jos_jxgallery_contests in FROM and INNER JOIN. In INNER JOIN you make a ambiguous connection. Why you try to do that?