I need to create a null value in SQL - mysql

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

Related

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

Add the column in mysql and has the same data like the column existing

I want to add the column name "payroll_date_on" in the table and has the default value as the other table column named "jo_time_on".
ALTER TABLE jobs ADD COLUMN payroll_time_on INT(11) AS (jo_time_on) PERSISTED;
While running this I got an error code
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 (jo_time_on) PERSISTED' at line 1
Help me to solve this error and clone the column jo_time_on
I am using the mysql version 5.6.38
try this:
ALTER TABLE jobs ADD COLUMN payroll_time_on INT(11);
UPDATE TABLE SET payroll_time_on = jo_time_on;

SQL Query having trouble creating temporary table

I am trying to use the my table adult3 to create a temporary table with rows from the column class that correspond to the condition:
SELECT class INTO #CLTable
FROM adult3
WHERE (class = '<=50K');
but I keep getting the 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
'FROM adult3
WHERE (class = '<=50K')' at line 2
I don't get what I'm doing wrong.
I believe that you are using SQL Server syntax for creating a temp table.
Try this:
create temporary table CLTable as
select class from adult3
where (class='<=50K');
You could try:
INSERT INTO [tempTableName]
SELECT class FROM adult3
WHERE (class='<=50k')

SQL Syntax Sum and Count

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)

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.