MySqlDataReader reads data when there is sum(a+b) in the query - mysql

When I use this query
("SELECT id, at_date AS MyDate , nom, pax, SUM(prix*pax) AS somme,
SUM(pax) AS totpax
FROM atelier ORDER BY " + Order_by + " " + SortDir + " LIMIT #Myid," +
PublicVariables.MySqlLimit, conn)
This gives an error here
_listBox[1].Items.Add(Convert.ToDateTime(reader["MyDate"]).ToString("d"));
Because there is NO ROWS int the table. Table is EMPTY.
But when I supress SUM(prix*pax) AS somme and SUM(pax) AS totpax from the query, reader does not read and no error occurs.
Is there any trick of MySql in there ?
I resolved the probleme by checking the table if there is any rows before calling this method but it's not what I like any idea ?

Use Group by clause for all non aggregate function before order by clause:
group by id,MyDate,nom,pax

Related

How to re-use result from a SELECT statement?

My goal is to re-use the result from a SELECT statement to be used in SQL EXISTS statement.
The general idea looks like this:
SELECT *
FROM table
WHERE col1=1
OR EXISTS (
SELECT 1 // this is an exact copy of the SELECT statement above.
FROM table
WHERE col=1
)
The actual SQL statement I am trying to reduce:
"SELECT user_detail.user, user_detail.name, channel_member.role " +
"FROM user_detail " +
"JOIN channel_member ON user_detail.user=channel_member.user " +
"AND channel_member.uuid=#{channelUuid} " +
"WHERE user_detail.user=#{username} " +
"OR EXISTS ( " +
" SELECT 1" +
" FROM user_detail " +
" JOIN channel_member ON user_detail.user=channel_member.user " +
" AND channel_member.uuid=#{channelUuid} " +
" WHERE user_detail.user=#{username} " +
")"
You can only do this if your version of MySQL supports window functions, ie. version >= 8.0
You can use conditional window aggregation, like this:
SELECT *
FROM (
SELECT *, COUNT(CASE WHEN col1 = 1 THEN 1 END) OVER () AS CountMatches
FROM table
) t
WHERE CountMatches > 0;
Depending on the number of rows matching to non-matching, this may be more or less performant. You need to test.
This query:
SELECT *
FROM table
WHERE col1 = 1 OR
EXISTS (SELECT 1 // this is an exact copy of the SELECT statement above.
FROM table
WHERE col=1
)
Doesn't really make sense. It is saying to return all rows if col = 1 is in the table -- but then it filters to check if any row has col = 1. So it is equivalent to:
SELECT *
FROM table
WHERE (SELECT 1 FROM table t2 WHERE t2.col = 1);
I strongly suspect that you intend NOT EXISTS -- so get everything with 1. If there is no 1 then return everything:
SELECT *
FROM table
WHERE col1 = 1 OR
NOT EXISTS (SELECT 1 // this is an exact copy of the SELECT statement above.
FROM table
WHERE col = 1
);
This should work fine with tables -- and is in fact probably optimal with the right indexes.
If "table" is really a complex query, then you might consider window functions:
select t.*
from (select t.*,
sum( col = 1 ) as num_1s
from t
) t
where col = 1 or num_1s = 0;
If you want to use any conditional statement on the query you are running you will need to wrap the query and put it in a FROM statement and then run the conditional outside of the query, like so....
SELECT aliasName.*
FROM
(SELECT *
FROM table
WHERE col1=1) aliasName
WHERE EXISTS aliasName //this is the conditionl statement OUTSIDE of the query you built.
Let me know how you do...
For Mr./Ms. Barmar:
The idea
// Idea:
// If one of the row have col1 with value 1.
// Then return all of the row, or return empty []
// SELECT *
// FROM table
// WHERE col1=1
// OR EXISTS (
// SELECT 1
// FROM table
// WHERE col=1
// )
The solution
"WITH temp AS (" +
" SELECT user_detail.user, user_detail.name, channel_member.role " +
" FROM user_detail " +
" JOIN channel_member ON user_detail.user=channel_member.user " +
" AND channel_member.uuid=#{channelUuid} " +
") " +
"SELECT * " +
"FROM temp " +
"WHERE user=#{username} " +
" OR EXISTS ( " +
" SELECT 1" +
" FROM temp " +
" WHERE user=#{username} " +
" )"
The solution above use WITH clause as recommended by Mr./Ms. Barmar, I am posting this, so you can inspect whether this is logical or not.

Passing values to the insert query with select query

I want to insert two values in the a table.One of which is actually taken from another table with the select statement as below.
query = "INSERT INTO empallowance(emp_id_fk,allowance_id_fk) VALUES(SELECT emp_id FROM employee WHERE emp_cnic='" + cnic + "',#allowance_id_fk)";
There is syntax error exception as shown in the figure.
Your SQL statement is invalid. Use the following:
query = "INSERT INTO empallowance SELECT emp_id, #allowance_id_fk FROM employee WHERE emp_cnic='" + cnic + "'";
You can read all about the approach here.
you have to use bracket in sub query.
try this:
query = "INSERT INTO empallowance(emp_id_fk,allowance_id_fk) VALUES((SELECT emp_id FROM employee WHERE emp_cnic='" + cnic + "'),#allowance_id_fk)";
You can modify your query as below :
query = "INSERT INTO empallowance(emp_id_fk,allowance_id_fk) SELECT emp_id, #allowance_id_fk FROM employee WHERE emp_cnic= ' " + cnic + "'";
Add '()' between select query for a separation of insertion query.
INSERT INTO empallowance(emp_id_fk,allowance_id_fk) VALUES((SELECT emp_id FROM employee WHERE emp_cnic='" + cnic + "'),#allowance_id_fk)
You can't do it that way but you can create a select statement and insert its results:
"INSERT INTO empallowance (emp_id_fk,allowance_id_fk)
select emp_id, #allowance_id_fk
from employee
WHERE emp_cnic='" + cnic + "'"
Also, take note, using string concatenation to insert the parameter is vulnerable for SQL Injections - Use parameterized queries instead
You can easily do this by this, it worked for me
query = "INSERT into TABLE1 (name,city)
Select name, 'Paris' from TABLE2 where id = 1";
you can assign values directly in a select query.

Conversion from type 'DBNull' to type 'Double' is not valid

In my code I understand query getting null values and it throws this error. But since my query is little complex I don't understand how do I check for null values and avoid this error. Please help me to correct this query.
SELECT (SUM(charges) + SUM(behaviour) + SUM(admission) + SUM(properInformation) + SUM(hygine) + SUM(treatment))/(count(doctorID) * 6) AverageRating, COUNT(ID) RatingCount from ratings where doctorID = '" + doctorID + "'
If you want the query to not return NULL, you can just surround the expression with IFNULL to convert a possible NULL to 0, something like;
SELECT IFNULL((SUM(charges) + SUM(behaviour) + SUM(admission) +
SUM(properInformation) + SUM(hygine) + SUM(treatment))
/(count(doctorID) * 6), 0) AverageRating,
COUNT(ID) RatingCount
FROM ratings
WHERE doctorID = '" + doctorID + "'
If you definitely know your query returning null value correctly, then you can use try-catch block as below:
Try
Dim dt As DataTable = Me.GetData("SELECT (SUM(charges) + SUM(behaviour) + SUM(admission) + SUM(properInformation)
Catch ex As Exception
MsgBox("Error while fetching data" & vbCrLf & ex.Message)
End Try

Adding string as a part of query in jdbcTemplate call in MySQL

I have a query and a few parameters as follows,
String query = "SELECT * FROM table_name ORDER BY ? LIMIT ? ";
//I am creating this 'sortString' on runtime based on some user inputs
String sortString = " column1 ASC, column 2 ASC ";
int count =5;
I am calling the jdbcTemplate method as follows,
List<Map<String, Object>> rows = getJdbcTemplate().queryForList(query, sortString, count);
The query that is actually used by the jdbcTemplate is as follows,
SELECT * FROM table_name ORDER BY ' column1 ASC, column 2 ASC ' LIMIT 5
Now, the ORDER BY clause does not works since the criteria is put up inside ' ' by jdbcTemplate. How can I add the string to the query without the jdbcTemplate adding the " ' " by default.
I want the query to be,
SELECT * FROM table_name ORDER BY column1 ASC, column 2 ASC LIMIT 5
You cannot use a prepared statement, when you generate a whole part of the query dynamically. The ? in a prepared statement always stands for a value.
//I am creating this 'sortString'
String sortString = " column1 ASC, column 2 ASC ";
String query = "SELECT * FROM table_name ORDER By " + sortString + " LIMIT ? ";

Delete query error for multiple records

I have a problem in this query:
string sqlString = "DELETE FROM [upload_news] WHERE (SELECT TOP " + no_of_recordss + " * FROM [upload_news] WHERE [country]='" + countryy.Text + "')";
Error Message :
Error: {"An expression of non-boolean type specified in a context
where a condition is expected, near ')'."}
How can i fix this ?
In the where clause you need a boolean expression.
Moreover, mysql doesn't support select top, you have to use limit instead and you can use it directly on delete
So your query should be:
delete from upload_news
where country=<SOME_COUNTRY> limit <NO_OF_RECORDS>
You have to replace values within "<>" with your desired values.
Or in your "strange" syntax:
string sqlString = "DELETE FROM [upload_news] WHERE [country]='" + countryy.Text + "' limit "+no_of_recordss;