SQL Syntax Sum and Count - mysql

I have a 1064 error:
You have an error in your SQL syntax; check the manual that correspond to your MySQL server version for the right syntax to use neear '*) as NB_FR, sum (*)) as MT_FR
FROM gc_mouvements where COOPX="477" or COOPX="4' at line 4
Here is the code I used, I don't understand the origin of the error:
ALTER TABLE gc_modele_retrait ADD COLUMN
(Nb_frais_477 int(5),Nb_frais_481 int(5),Mt_frais_477 int(5),Mt_frais_481 int(5));
UPDATE gc_modele_retrait
SET Nb_frais_477=0, Nb_frais_481=0, Mt_frais_477=0, Mt_frais_481=0;
DROP TABLE IF EXISTS gc_modele_retrait_frais;
CREATE TABLE gc_modele_retrait_frais
(PRIMARY KEY (COCO))
ENGINE=myisam
SELECT COCO, COOPX, COUNT(*) AS NB_FR, SUM(*) AS MT_OPE
FROM gc_mouvements WHERE COOPX="477" OR COOPX="481" GROUP BY COCO, COOPX;

The issue is with using SUM (*), you have to pass numerical value or column containing numerical value to SUM. In your case you need:
SUM (Nb_frais_477 + Nb_frais_481)

Related

I need to create a null value in SQL

I am tasked to create a column with a null value but I got an error.
SELECT * FROM test.Persons
ALTER TABLE Persons
ADD mark_percentage FLOAT
This is my error.
12:32:47 use database test ALTER TABLE PERSONS ADD MARKS float SELECT * FROM test.Persons 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 'database test ALTER TABLE PERSONS ADD MARKS float SELECT * FROM test.Persons' at line 1 0.063 sec
Try this :
ALTER TABLE Persons
ADD mark_percentage FLOAT

Select ALL columns in MySQL with one column inside CAST function

I basically want to select all columns of my MySQL table, but want to change the datatype of only one column namely Patient_Number using CAST function only. Here is a screenshot of my MySQL table
So as my output, I want a similar table as I have shown in the screenshot, just want to have the datatype of Patient_Number from INT to VARCHAR.
I tried executing the following queries:
select * cast(Patient_Number as varchar) from clinic_data;
select * from clinic_data cast(Patient_Number as varchar);
But only got the following error message:
ERROR 1064 (42000): 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 'cast(Patient_Number as char)' at line 1
SELECT * is basically not a good idea
But you need a comma, between both statements
And it should be char for the cast
select *, cast(Patient_Number as char) from clinic_data;
db<>fiddle here

error while creating table in mysql for generating auto serial number

I just want to create the serial number into the table not just for view only for example:
table ConfirmationNumber:
SerialNo
--------------
00000001
00000002
00000003
and so on (the serial number will be increase each time i execute the query).
For that when I am creating a table in sql as:
CREATE TABLE confirm
(
ConfirmationID int,
name varchar(10),
SequenceNumber AS RIGHT('0000000' + convert(varchar, ConfirmationID), 8)
)
it's giving an error:
ERROR 1064 (42000): 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
RIGHT('0000000' + convert(int, ConfirmationID), 8)
)' at line 9
Can any body help me?
Remove sql-server from tags.
Put the desired code in your INSERT statements, or use a TRIGGER.
You can also use the ZEROFILL attribute to get 0s automatically displayed.

Debugging MySQL CREATE TABLE from existing table statement?

Why is this giving me an error ?
I haven't found any good example code online that combines CREATE TABLE with a SELECT statement with WHERE.
CREATE TABLE tmp_year (source CHAR(3),
target CHAR(3),
val FLOAT,
PRIMARY KEY (source, target))
(SELECT source,
target,
val
WHERE date>='2001-01-01'
AND date<='2001-12-12')
FROM db;
error message:
ERROR 1064 (42000): 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 date>='2001-01-01' AND date<='2001-12-12') FROM db' at line 1
Try this my friend:
CREATE TABLE tmp_year AS
SELECT * FROM YOURTABLE
WHERE date>='2001-01-01'
AND date<='2001-12-12';
ALTER TABLE tmp_year ADD PRIMARY KEY(source, target);
Here is example in SQL Fiddle
SQL expects FROM to immediately follow a SELECT clause's values and be before conditionals like WHERE.

MySQL You can't specify target table for update in FROM clause

When I run this query
update Apply
set major='CSE'
where major='EE' and sID in (select sID from Student
where GPA >= all (select GPA from Student
where sID in (select sID from Apply
where major='EE')));
it returns an error: ERROR 1093 (HY000): You can't specify target table 'Apply' for update in FROM clause. But it succeeds in another MySQL.
if I add "as tmp" to the end of the code, it returns an error: ERROR 1064 (42000): 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 tmp' at line 6
Can somebody tell me the reason and how can I modify the code?
Thank you very much!
Problem: You're referencing the table Apply twice in two different contexts.
Solution: Give them different aliases, e.g. Apply as a1 and Apply as a2
(Maybe this also needs to be done for Student)