can't figure out what's wrong with this sql query - mysql

Trying to get a list of the latest forum posts from a user, ordered by the date it was made.
The problem is posts are split between topics and replies, so i need to get the reply text from a different table if the latest post isn't the topic itself.
Trying to use CASE to switch between a subquery if it's a reply else use the topic text.
SELECT
t.`topic_id`,
t.`topic_title`,
t.`last_post_date`,
t.`last_post_id`,
CASE WHEN t.`replys` > 0 THEN (
SELECT
`reply_text` AS 'text'
FROM
`forum_replies`
WHERE
`post_id` = t.`last_post_id`
) ELSE t.`topic_text` AS 'text'
END
FROM
`forum_topics` t
WHERE
t.`approved` = 1 AND t.`forum_id` IN (1) AND t.last_post_user_id = 1
ORDER BY
t.`last_post_date`
DESC
LIMIT 5
The error is:
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 'text' END FROM
forum_topics t WHERE
t.approved = 1 AND t.`f' at line 13

Use of subquery in CASE is not allowed. Use LEFT JOIN instead:
SELECT
t.`topic_id`,
t.`topic_title`,
t.`last_post_date`,
t.`last_post_id`,
t.`reply_text` as 'text'
FROM
`forum_topics` t
LEFT JOIN `forum_replies` r on r.`post_id` = t.`last_post_id`
WHERE
t.`approved` = 1 AND t.`forum_id` IN (1) AND t.last_post_user_id = 1
ORDER BY
t.`last_post_date`
DESC
LIMIT 5

Related

Where is my mistake selecting coalesce mysql?

I have a column discount_amount, if there's no SUM(discount_amount) (NULL) THEN it's zero (0).
I wrote the query with this
SELECT COALESCE(SUM(discount_amount)
FROM order_discount
WHERE order_discount.discount_type_id = 6
AND order_discount.order_match_id = om1.id, 0);
but I get an error
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 'FROM order_discount WHERE order_discount.discou' at line 2
You seem to want:
SELECT COALESCE(SUM(discount_amount), 0)
FROM order_discount
WHERE order_discount.discount_type_id = 6 AND order_discount.order_match_id = om1.id;
This has to be part of bigger query, where derive table om1 is defined somehow.
Alternatively, you can COALESCE() the result of the subquery like this:
COALESCE(
(
SELECT SUM(discount_amount)
FROM order_discount
WHERE order_discount.discount_type_id = 6 AND order_discount.order_match_id = om1.id
),
0
)
Again, this only makes sense if included in a bigger query.

mysql query with Or, Left Join, Outer Left Join, AND ON and IS NULL

I'm trying to pull records from one table (workers) that matches 2 of 5 status options (Active or Pending), within a specific date range, and I want to identify from a separate table (work_earn) a specific payment method (ACH), and where they have not yet been paid (IS NULL)
I've tried a few different ways, but I keep getting a syntax error. What am I missing? Here are two queries that I tried, with corresponding error messages.
SELECT * FROM workers
WHERE (status = 'Active') OR (status= 'Pending')
and date >= '$Start' AND date <= '$End' AND
LEFT JOIN work_earn method = 'ACH' AND ON work_earn.wid IS NULL
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 work_earn method = 'ACH' AND ON work_earn.wid IS NULL LIMIT 0, 30' at line 1
SELECT * FROM workers
WHERE status = 'Active' OR 'Pending' and date >= '2018-04-18' AND date <= '2018-03-18' AND
LEFT JOIN work_earn method = 'ACH' AND ON work_earn.wid IS NULL
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 work_earn method = 'ACH' AND ON work_earn.wid IS NULL LIMIT 0, 30' at line 1
Any help would be greatly appreciated!

Querying SQL Set

I have an SQL field defined as set('nightlife', 'food', 'sports', 'culture', 'movies', 'general')
Now I want to run a query where I pass for example nightlife,food and I want the result to contain ALL records where the Category contains nightlife or food. So for example a record with nightlife,culture,sports should also be returned as it contains nightlife. I'm not sure how to run this. I tried using the IN keyword in the following way:
'SELECT ... FROM table WHERE '$category' IN Categories
however this isn't working.
UPDATE
Running following query as suggested in answer:
SELECT *
FROM images
WHERE id = '2650225'
AND WHERE FIND_IN_SET('sports', Categories ) >0
OR FIND_IN_SET('nightlife', Categories ) >0
ORDER BY delete_at ASC
LIMIT 10
OFFSET 0
receiving 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 'WHERE find_in_set('sports', Categories) > 0 or find_in_set('nightlife', Categori' at line 1
You can use find_in_set with or:
select *
from yourtable
where find_in_set('nightlife', categories) > 0 or
find_in_set('food', categories) > 0
SQL Fiddle Demo
Based on your edits, you can't have multiple where clauses. Also you need to use parentheses around the or criteria:
SELECT *
FROM images
WHERE id = '2650225' AND
(FIND_IN_SET('sports', Categories ) > 0 OR
FIND_IN_SET('nightlife', Categories ) >0)
ORDER BY delete_at ASC
LIMIT 10
OFFSET 0

MySQL 'partition by' clause

I am getting error in the partition clause. Please help me. The error is
Select Model_Name,
Case Model_Subtype
When 'Weight' then 'Static'
When 'Linked Account' then 'Dynamic'
When 'Flexible' then 'Dynamic'
Else 'Not Defined'
End as Model_Type, Security_Name, Market_Value,Weight,
Case When Weight = 0 And Market_Value= 0 Then 0
When Weight = 0 Then Cast(Market_Value/ nullif(SUM(market_Value)
OVER (Partition by Model_Name),0) AS Decimal (10,4))
When Weight !=0 Then Weight/100
Else Weight End as Target_Weight,
vehicle.Vehicle_Name
from
OFF_PRODUCT_MODEL model
Join OFF_PRODUCT_MODEL_TARGET target on target.Model_Id = model.Model_Id
Join OFF_PRODUCT_SECURITIES Sec on sec.Security_Id = target.Security_Id
left outer Join Offer_Back_End.tblStrategies Strategy on Strategy.Vestmark_Sleeve_Code = model.Model_Name
left outer join Offer_Back_End.tblVehicles vehicle On vehicle.Vehicle_Id = strategy.Vehicle_ID
Where (Strategy.Active_Strategy is null or Strategy.Active_Strategy = 1)
Err] 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 '(Partition by Model_Name),0) AS Decimal (10,4))
When Weight !=0 Then Weight/' at line 11
Umm, OVER (Partition by Model_Name) is analytical function which MySQL unfortunately doesn't support and don't have them and so you are getting the said error.
MySQL does not support OVER() clause. In general Window Functions are not part of MySQL.
You can however emulate it - please refer to the following answer:
https://stackoverflow.com/a/20432371/900564

error in updating mysql table with filters

i have a mysql table with the follwoing fields :
id, desc, value, people, amount, weight
in the above order i run the follwoing
update match1 set weight = 5 where desc = 'fat' and id != '6';
follwoing is the error message i get :
**#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 'desc = 'bat' and id = 6' at line 1
can someone please let me know whats wrong with this?
the column desc is a keyword in mysql. use backquotes i.e.
where `desc` = 'fat'