ALTER TABLE customer_rewards
ALTER COLUMN count(rental_id) total_rentals
bigint(21);
I'm trying to change name of the column: count(rental_id) to total_rentals
MySql recognizes count as a function instead of a column name. I can't go in and change the name where you create the table, I can only use DDL to modify existing columns. I've tried using quotes (both single and double) and it still isn't working. How can I get around this?
I'm afraid to ask how that table ended up with such a column name... Anyway: this identifier contains parentheses, so it needs to be quoted: for this you need to use backticks in MySQL:
ALTER TABLE customer_rewards ALTER COLUMN `count(rental_id)` total_rentals bigint(21);
Related
I need to select a value from a column in the table as a name of the another column in mysql
for ex ::
select column AS (select column from table where id = 1) from table;
it give me a syntax error .. How can I use select statement inside AS Function
Actually I need to set a value from a column as a name to another column using AS Function in mysql
The answer is simple: It is not possible in SQL. Column aliases are constants.
You can't do this in a single query. All identifiers must be fixed in the query at the time it is parsed, which is before the query begins reading data. This includes column names and column aliases. The names or aliases of columns cannot be set at runtime based on data read during the query.
But you can do what you want in two queries.
Query to get the column alias name you want to use. This returns a string.
Use that string as you format the second query. Then the column alias will be fixed in that second query by the time you prepare it.
Let's say I have a table like this
this table is the result of a query from another larger table stored in my database
All I want is to create a table like this one above and specify for each column a custom format and store it into my database
I know that I could do create table mytab as select ... etc
however i don't know how to specify the column formats that I want in mysql
could you please help ?
If you have the query sql, you should be able to do a select into to store the results in a table. Add a LIMIT clause to just store one row. You could then do SHOW CREATE TABLE tablename (from this SO answer) to get the SQL for creating the table. It would be up to you to figure out what your primary key should be.
Assuming with column formats you mean data types: Use CAST to cast to the desired data type.
create new_table as
select
cast( a.metrique as varchar(100) ) as metrique,
cast( b.nombre_de_lignes as int ) as cote_de_lignes, ...
from ...
You may specify columns properties completely or partially. Like there is no SELECT part, and you simply create empty table.
I.e. like
CREATE TABLE table_name ({any definitions allowed in table creation query:
columns specifications, indices, constraints, FKs, etc.})
SELECT ...
In this form each output column in SELECT must have alias which matches according column name defined in CREATE TABLE part. If alias is absent in the structure then a column with the name==alias will be added to the table definition with dynamically formed properties.
I have a stored procedure in which the data is populated into the destination table using a temporary table(in this i perform all the computation).
the problem i am facing is i have two columns which are getting its value by concatenating two other columns.
i.e.
concat(status_code|status_reason) as STATUS
concat(prior_status_code|prior_status_reason) as PRIOR_STATUS
Now this concatenation i have done it in temporary table.
i have to load its data into a destination table and the destination table doesn't have the columns STATUS and PRIOR_STATUS.
for loading the data i am using
insert into Destination( column_names,Status,prior_status)
select (column_names) from temporary table
so when i execute this part it throws me an error that columns are invalid as not present in the table.
P.S. I have used alter command to add those two columns in the destination. Still not getting the desired result.
Is there any other way to do this and where am i going wrong in this?
please help.
Thanks in advance
try this to add you field. it is important that you put the fieldnames in Backquotes. status is a reserved word.
ALTER TABLE youTable
ADD COLUMN `status` VARCHAR(100),
ADD COLUMN `PRIOR_STATUS` VARCHAR(100);
you can also specify the position of the field in the table like:
ALTER TABLE youTable
ADD COLUMN `status` VARCHAR(100) AFTER fieldxx,
ADD COLUMN `PRIOR_STATUS` VARCHAR(100) BEFORE fieldzz;
I want to create a new table with rows of the column names. Essentially, I want to put the output of a decribe statement into a new table, so that I can use a where clause to only extract certain column names. I'm using sparksql.
How do I do this? Thanks.
No need to create an additional table just use either SHOW COLUMNS command instead of describe with filter, or query information_schema.columns table with a select.
I had a table name called emp-reg in mysql.When I gave desc emp-reg it showed me an error.
Where as when I changed the table name RENAME TABLE emp-reg TO emp and then gave desc emp it worked perfectly.Why is desc not working for-
what will happen if we perform insert operation in table such asemp-reg?
Instead, you can use _ under score.
- is treated as minus sign and hence is rejected.
Or you can use back ticks around the name with - in it. Say
`emp-reg`.
If you want to rename any of such columns in your table, use alter table command with change option.
ALTER TABLE table_name CHANGE COLUMN `old-col-name` `new_col_name` int
You can use any desired or matching data type for the column with the earlier definition.