Multiple-table UPDATE with WHERE not working - mysql

Can someone help me understand why below UPDATE query produces an ERROR?
WITH subt AS (
SELECT t.portfolio, s.isin, t.quantity, t.date
FROM transactions t
JOIN stocks s
ON t.stock = s.name
ORDER BY t.id DESC
LIMIT 1
)
UPDATE holdings h
JOIN subt
ON h.portfolio = subt.portfolio
AND h.isin = subt.isin
SET h.end_date = DATE_SUB(subt.date, INTERVAL 1 DAY)
WHERE h.end_date is NULL
The error I get:
SQL Error (1064): You have an error in your SQL syntax; check the
manual that corresponds to your MariaDB server version for the right
syntax to use near 'UPDATE holdings h JOIN subt ON h.portfolio =
subt.portfolio AND h.isin ' at line 10
While below SELECT works fine:
WITH subt AS (
SELECT t.portfolio, s.isin, t.quantity, t.date
FROM transactions t
JOIN stocks s
ON t.stock = s.name
ORDER BY t.id DESC
LIMIT 1
)
SELECT h.*
FROM holdings h
JOIN subt
ON h.portfolio = subt.portfolio
AND h.isin = subt.isin
WHERE h.end_date is NULL
I'm working with a MariaDB 10 Database via HeidiSQL

Instead of a WITH result you could try using a normal inner join on subquery
UPDATE holdings h
JOIN
(
SELECT t.portfolio, s.isin, t.quantity, t.date
FROM transactions t
JOIN stocks s
ON t.stock = s.name
ORDER BY t.id DESC
LIMIT 1
) subt
ON h.portfolio = subt.portfolio
AND h.isin = subt.isin
SET h.end_date = DATE_SUB(subt.date, INTERVAL 1 DAY)
WHERE h.end_date is NULL

Related

Mysql sub-query return null

Hi I am a beginner I am trying to make all the result row of my subquery that is null to return 0 not null. but I am getting an error. I will really appreciate any advice. Thank you
Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS 'Income'
SELECT
COUNT(DISTINCT t1.id) AS 'Val1',
COALESCE((SELECT SUM(CAST(COALESCE(r.t_payment_total,0) AS DECIMAL(18,2))) AS 'Income'
FROM reserv r
INNER JOIN newtbladds1 t ON t.t_parent_id = r.id
WHERE r.t_status!="Pending" && r.t_status!="Booked" AND r.c_mid = m.id AND t.t_type_id = t1.t_type_id
),0)AS 'Income'
FROM tbladds1 t1
JOIN tbladds1_type tt ON tt.id = t1.t_type_id
JOIN tbladdress m ON m.id = t1.t_mid
JOIN tbladdressfr mf ON mf.id = t1.t_floor_id
JOIN tblppl mp ON mp.t_mid = m.id AND mp.t_type = 'try' AND mp.t_system_id = 'ok'
GROUP BY t1.t_tool_type_id
ORDER BY m.t_m ASC, tt.t_ttype ASC, mf.t_floor ASC;
Output I removed COALESCE I am having null
Val1 Income
10 Null
2 30
23 10
5 Null
Desired Output
Val1 Income
10 0
2 30
23 10
5 0
I would suggest moving that subquery into the from clause
SELECT
t1.t_tool_type_id
, COUNT( DISTINCT t1.id ) AS `Val1`
, COALESCE(i.Income , 0 ) AS `Income`
FROM tbladds1 t1
JOIN tbladds1_type tt ON tt.id = t1.t_type_id
JOIN tbladdress m ON m.id = t1.t_mid
JOIN tbladdressfr mf ON mf.id = t1.t_floor_id
JOIN tblppl mp ON mp.t_mid = m.id
AND mp.t_type = 'try'
AND mp.t_system_id = 'ok'
left join (
SELECT
CAST( SUM(r.t_payment_total) AS decimal(18, 2) ) AS `Income`
FROM reserv r
INNER JOIN newtbladds1 t ON t.t_parent_id = r.id
WHERE r.t_status != 'Pending'
AND r.t_status != 'Booked'
AND r.c_mid = m.id
GROUP BY
t.t_type_id
) as i on t1.t_type_id = i.t_type_id
GROUP BY
t1.t_tool_type_id
;
nb: I have assumed r.t_payment_total is numeric.
I also suggest you always use single quotes for values/literals, and in MySQL backticks for identity (e.g. column headings) but really they are not essential in this query.

Results returning twice from MySQL query

I'm struggling to figure out why my results are returning twice for the group messages. It's returning the correct values for the single conversations.
It should be returning all the data from Data in table screenshot. However it's returning the data with is_group = 1 twice.
Data in Table:
MySQL Query:
(SELECT dm.* FROM `direct_message`AS dm
INNER JOIN direct_message_thread AS dmt
ON dmt.chat_id = dm.id
WHERE
( dm.recipient_id = '10896' OR dm.creator_id = '10896' )
AND dm.school_id = '1'
GROUP BY dm.id
ORDER BY dmt.inserted DESC
) UNION ALL (
SELECT dm.* FROM `direct_message` AS dm
INNER JOIN direct_message_thread AS dmt ON dmt.chat_id = dm.id
LEFT JOIN direct_message_group AS dmg ON dmg.chat_id = dm.id
WHERE dmg.staff_id = '10896' AND dm.school_id = '1'
GROUP BY dm.id
ORDER BY dmt.inserted DESC
) LIMIT 0, 25
Results:
I think it could be because of the first SELECT getting the results and then the UNION ALL get the same results but not grouping together with the first SELECT
When I try and do the following:
(SELECT dm.* FROM `direct_message`AS dm
INNER JOIN direct_message_thread AS dmt
ON dmt.chat_id = dm.id
WHERE ( dm.recipient_id = '10896' OR dm.creator_id = '10896' )
AND dm.school_id = '1'
) UNION ALL (
SELECT dm.* FROM `direct_message` AS dm
INNER JOIN direct_message_thread AS dmt ON dmt.chat_id = dm.id
LEFT JOIN direct_message_group AS dmg ON dmg.chat_id = dm.id
WHERE dmg.staff_id = '10896' AND dm.school_id = '1'
)
GROUP BY dm.id
ORDER BY dmt.inserted DESC
LIMIT 0, 25
It shows this error message:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'GROUP BY dm.id ORDER BY dmt.inserted DESC LIMIT 0, 25' at line
14
You are using union for two queries.
Records 595-597 and 599-601 are met both of them.
My be better to select to whole query with
select id,...
from (...)
group by id

Mysql error in order by desc

this is my query.when i add order by desc in this query its gets an error pls help me.
SELECT *
FROM
(SELECT package_details.*,
r.state AS source_name,
d.state AS dest_name
FROM (`package_details`)
LEFT JOIN country_state_city r ON r.id=package_details.region_id
LEFT JOIN country_state_city d ON d.id=package_details.destination_id
WHERE `package_availability_type` = 'all'
AND `admin_status` = 'ACTIVE'
AND `status` = 'ACTIVE'
AND (package_name LIKE '%Istanbul%'
OR routes LIKE '%Istanbul%'
OR r.state LIKE '%Istanbul%'
OR d.state LIKE '%Istanbul%')
AND
ORDER BY `package_price` DESC
UNION ALL SELECT package_details.*,
r.state AS source_name,
d.state AS dest_name
FROM (`package_details`)
LEFT JOIN country_state_city r ON r.id=package_details.region_id
LEFT JOIN country_state_city d ON d.id=package_details.destination_id
WHERE `package_availability_type` ='range'
AND `admin_status` = 'ACTIVE'
AND `status` = 'ACTIVE'
AND `to_date` >= '2017-02-07'
AND (package_name LIKE '%Istanbul%'
OR routes LIKE '%Istanbul%'
OR r.state LIKE '%Istanbul%'
OR d.state LIKE '%Istanbul%')
AND
ORDER BY `package_price` DESC) AS dt LIMIT 0,100
error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY package_price DESC UNION ALL SELECT package_details.*,r.state AS sour' at line 1
Remove AND Before Order By Clause
AND
ORDER BY `package_price` DESC

MySQL throwing error on second JOIN

I'm getting the following error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'JOIN product_catalog ON product_catalog.entity_id
As a result of the following query:
SELECT sales_order.created_at , order_item.order_id, sales_order.increment_id, SUM(order_item.qty_ordered) AS qty_ordered , COUNT( * )
FROM order_item
JOIN sales_order
ON sales_order.entity_id = order_item.order_id
WHERE sales_order.created_at > '2012-11-15 00:00:00'
JOIN product_catalog
ON product_catalog.entity_id = order_item.product_id
WHERE product_catalog.size = 14
GROUP BY order_item.order_id;
Variations on this query have worked for grouping different types of product by sales order in the past where I only needed to perform one JOIN to get all the info I needed. The problem I'm encountering is from the second JOIN. Clearly I'm missing something but I really am not sure what. :(
Please make sure that WHERE condition must be after all JOIN
SELECT sales_order.created_at , order_item.order_id, sales_order.increment_id, SUM(order_item.qty_ordered) AS qty_ordered , COUNT( * )
FROM order_item
JOIN sales_order
ON sales_order.entity_id = order_item.order_id
JOIN product_catalog
ON product_catalog.entity_id = order_item.product_id
WHERE product_catalog.size = 14
AND sales_order.created_at > '2012-11-15 00:00:00'
GROUP BY order_item.order_id;
First of all you have to JOIN your tables which you need. Then after WHERE clause come for conditions.
Your WHERE clauses are in the wrong spots. See the code below for proper JOIN syntax.
SELECT sales_order.created_at,
order_item.order_id,
sales_order.increment_id,
SUM(order_item.qty_ordered) AS qty_ordered,
COUNT( * )
FROM order_item
JOIN sales_order
ON sales_order.entity_id = order_item.order_id
AND sales_order.created_at > '2012-11-15 00:00:00'
JOIN product_catalog
ON product_catalog.entity_id = order_item.product_id
AND product_catalog.size = 14
GROUP BY order_item.order_id
JOIN...ON... clause it's also section to input condition so you don't need where clause, just add AND instead.
SELECT sales_order.created_at , order_item.order_id, sales_order.increment_id,
SUM(order_item.qty_ordered) AS qty_ordered , COUNT( * )
FROM order_item
JOIN sales_order ON sales_order.entity_id = order_item.order_id
and sales_order.created_at > '2012-11-15 00:00:00'
JOIN product_catalog ON product_catalog.entity_id = order_item.product_id
and product_catalog.size = 14
GROUP BY order_item.order_id;
Please consider below example I added aliases. It's good practice to use it because code is more readable.
SELECT SO.created_at , OI.order_id, SO.increment_id,
SUM(OI.qty_ordered) AS qty_ordered , COUNT( * )
FROM order_item OI
JOIN sales_order SO ON SO.entity_id = OI.order_id
and SO.created_at > '2012-11-15 00:00:00'
JOIN product_catalog PC ON PC.entity_id = OI.product_id
and PS.size = 14
GROUP BY OI.order_id;

mysql query Join table doesn't work

here is my query that makes an error for Join
$query = 'SELECT
a.ks_u_id,
a.ks_keyword,
b.u_photo
FROM
'.T_KEYWORD_HISTORY.' a
WHERE a.ks_u_id in ( SELECT uf_target_id FROM '.T_USER_FOLLOW.' WHERE uf_user_id="'.$u_id.'" and uf_target_id <> "'.$u_id.'" )
JOIN '.T_USER_ACCOUNT.' b ON b.u_id = a.ks_u_id
ORDER BY a.ks_time DESC
LIMIT 0 , 5 ';
I get this error message.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'JOIN T_USER_ACCOUNT b ON b.u_id = a.ks_u_id ORDER BY a.ks_time DESC ' at line 8
SELECT
a.ks_u_id, a.ks_keyword, b.u_photo
FROM T_KEYWORD_HISTORY a
WHERE a.ks_u_id in ( SELECT uf_target_id
FROM T_USER_FOLLOW
WHERE uf_user_id="jake" and uf_target_id <> "jake" )
JOIN T_USER_ACCOUNT b ON b.u_id = a.ks_u_id
ORDER BY a.ks_time DESC
LIMIT 0 , 5
It looks like I wrote wrong query for JOIN command.
Try
SELECT
a.ks_u_id, a.ks_keyword, b.u_photo
FROM T_KEYWORD_HISTORY a
JOIN T_USER_ACCOUNT b ON b.u_id = a.ks_u_id
WHERE a.ks_u_id in ( SELECT uf_target_id
FROM T_USER_FOLLOW
WHERE uf_user_id="jake" and uf_target_id <> "jake" )
ORDER BY a.ks_time DESC
LIMIT 0 , 5
The JOIN clause comes before the WHERE clause. Your query should be:
SELECT
a.ks_u_id, a.ks_keyword, b.u_photo
FROM T_KEYWORD_HISTORY a
JOIN T_USER_ACCOUNT b ON b.u_id = a.ks_u_id
WHERE a.ks_u_id in ( SELECT uf_target_id
FROM T_USER_FOLLOW
WHERE uf_user_id="jake" and uf_target_id <> "jake" )
ORDER BY a.ks_time DESC
LIMIT 0 , 5