SQL query for adding up columns - mysql

| ORDERID | subtotal |
--------------------
| 1000 | 50 |
| 1000 | 100 |
| 1001 | 75 |
Need a query that sum up all the prices when the orderid is 1000 and return 150.
I've tried sum function with where clause but its not working.
Here is my code:
public double findTotalSubtotal(String order_id) {
double sum = 0 ;
PreparedStatement ps;
try {
ps = connect.prepareStatement("select sum(subtotal) from order where ORDERID = ?");
ps.setString(1,order_id);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
int c = rs.getInt(1);
sum = sum + c;
}
}catch(Exception ex) {
ex.printStackTrace();
}
return sum;
}
Any my error:
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 'order where
ORDERID = '1000'' at line 1

Edited to include actual table name.
With the additional info of what your table name is, I can see what the problem is. "order" is a SQL keyword, so you can't use it as-is as a table name in a query. You have to surround it with square brackets backtics (because MariaDB):
select sum(price) from `order` where ORDERID = 1000
This assumes that the price is stored as a numerical type. Normally that's a pretty safe assumption, but if you're storing it as a VARCHAR, this won't work (and you should probably change the table structure to use a more appropriate type).

Related

How to concat two rows into string from a result set in MySql?

Basicly this is my mysql query:
select distinct(shipment_tag) from ir_shipment_registry where shipment_id = '2020111'
and the result set:
| shipment_tag |
+--------------+
| Truck |
| Equipment |
| |
How can I concat the two result set into string so that i can assign it to a variable? I tried
SET #purchasetype = (select distinct(shipment_tag) from ir_shipment_registry where shipment_id = '2020111')
but it returns and error says: Subquery returns more than 1 row.
I want something in my variable like : #purchasetype = "Truck, Equipment".
Perhaps use GROUP_CONCAT here:
SET #purchasetype = (SELECT GROUP_CONCAT(shipment_tag SEPARATOR ', ') FROM ir_shipment_registry WHERE shipment_id = '2020111');

SQL user defined function not run

I have situation where I want to generate invoice_id sequential for different product for multiple cities. I want to get generated invoice id according to different product and city.
My table temp look like
id |order_id |product|city|invoice_id
1 | 123 | 1 | 1 | FPU1
2 | 124 | 6 | 1 | PPU1
I want to get next invoice_id for product 1 and city 1 is FPU2.
For product 1 and city 2 is FBN1,product 6 and city 1 is PPU2 and so on ....
I create function but not run.Is anything wrong in function?
CREATE function generate(p_id INT, c_id INT)
returns VARCHAR(50)
BEGIN
-- DECLARE v_new_id VARCHAR(50);
SELECT Concat(( CASE
WHEN t.product = 1 THEN "f"
WHEN t.product = 6 THEN "p" end ),
c.city_name, Cast(RIGHT(t.invoice, Length(t.invoice) - 3) AS UNSIGNED) + 1
) v_new_id
FROM temp AS t
JOIN city c
ON c.city_id = t.city
WHERE t.product = p_id
AND t.city = c_id;
RETURN( v_new_id );
end;
Get syntax 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 '' at line 15
###Line 15 is AND t.city = c_id;
Need to set delimiters .
Thanks #P.Salmon for valuable comment and link
For set delimiter manually follow
For phpmyadmin user follow this link

MySql whole query returns Error 1690 while parts of it doesn't

I know that Error 1690 is nothing new in mysql but after searching for solution here i just gave up. So here I go, the error I get is:
BIGINT UNSIGNED value is out of range in '(v_from#10 - p_from#1)'
The problem is, I do not have any substraction in my query... honestly i even removed "-" from DATE_FORMAT!
My query is:
SET sql_mode = 'NO_UNSIGNED_SUBTRACTION';
select count(distinct user_id) as 'event_count_dis',count(user_id) as 'event_count',DATE_FORMAT(event_time,'%Y%m') as 'data',
getEventCourse(event_desc) as 'Name'
from sys_events c where DATE_FORMAT(event_time,'%Y%m') like '201809'
and event_type in ('Clicked')
group by getEventCourse(event_desc)
order by count(distinct user_id) desc Limit 20
Function getEventCourse(event_desc) works perfectly as it is very simple.
When i dissect this query and tested every part of it separately, everything worked fine... and I just gave up... Maybe some of you have any idea what's wrong? or at least can tell me what the heck is
'(v_from#10 - p_from#1)'
Function getEventCourse:
BEGIN
DECLARE courseName varchar(100);
IF(LOCATE('cid',description) > 0) THEN
SET courseName = (Select courses_name from sys_courses where courses_id = test_baza.extract_json_value(description,'cid'));
ELSEIF(LOCATE('course_id',description) > 0) THEN
SET courseName = (Select courses_name from sys_courses where courses_id = test_baza.extract_json_value(description,'course_id'));
ELSE
SET courseName = null;
END IF;
RETURN courseName;
END
sys_events:
event_id | event_time | event_desc | event_type | user_id
1537919 | 2018-10-12 | {"course_id":"386","element_id":"498"}| Clicked | 1235
Edit:
Ok, it seems that problem appears in "group by" section.

Executing an update statement with a select subquery clause in C

I have the following sql that I run in C:
snprintf(sql, 200, "update rec set name = (select name from pers where id = %d )
where id = %d",rec_id , emp_id );
mysql_query(conn, sql) returns a successful result but it's putting 1 in the "rec" table in the "name" field instead of the name, but when I printf the output and use it in MySQL it's working fine.
update rec set name = (select name from pers where id = 104 ) where id = 43
Is there something wrong with my sprintf? Or something has to be added?
I also tried static sql command like this
snprintf(sql,"update rec set name = (select name from pers where id = 104 ) where id = 43");
and it also put 1 in the rec.name
Is that due to count of record returned by the sub query? Can you verify by putting a condition which returns e.g. 2 records so that the name is set to 2? if this is the reason then (though less performing approach) try splitting the queries and see if it works this time.

Null Values In Linq to sql

I have table called Customers and this customer has Bank Details
but Some customer they don't have,
When i Use Linq to sql it's Return Null value, like a below example Table
How to prevent this null into Default value 0 or String
Ex. Table
Customer ID Name bank name
----------- ------ --------------
J0002 John HSBC
K0001 Kethy SMC
L0003 Mike Null
S0004 Lilie Null
Thanks
Something like
var customers = (from c in Customers
select new Customer
(
c.CustomerId,
c.Name,
c.BankName ?? ""
)).ToList();
or
var result = Customer.Select(x => new
{
CustomerId = x.CustomerId,
Name = x.Name,
BankName = x.BankName.HasValue ? x.BankName : ""
}).OrderBy(p => p.Name).ToArray();
could do the trick. It would be easier if you showed your current query.