Issue with MySQL Generated Columns - mysql

Hello I am trying to make a alter an exsisting table to add a Generated columns which is the count of all the rows in another table(It will be a like system so I am going to do all the matching and "WHERE" once I get this to work)
I am currently using this.
ALTER TABLE board ADD like_cnt INT GENERATED ALWAYS AS (COUNT(*) FROM likes) NOT NULL;
But it gives me this 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 'FROM likes) NOT NULL' at line 1
I am using WAMP and it has SQL version 5.7.24
Is this not possible or am I doing something wrong ?

There are limits in GENERATED COLUMNS notably 'Subqueries are not permitted'

Related

Adding a column to SQL with a formula

Apologies I'm brand new to SQL. I'm trying to add a column to SQL that calculates the number of days difference between as shipping date and todays date.
The following works perfectly when I want to view the days
SELECT DATEDIFF(now(),shipping_date) from tracking as days_transit
But when I try to make a new column with the following code I get errors
alter table tracking add days_transit as DATEDIFF(now(),shipping_date)
#1064 - 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 'as cast(DATEDIFF(now(),shipping_date))' at line 1
What am I doing wrong?! I am using phpmyadmin
Just alter table to add the new column first with no values. Then next update the column values accordingly with update command.

MySQL vs MariaDB - ddl - setting default for time field

I've got table products, and want to add column with type time.
I've got statement as follows:
ALTER TABLE products ADD openTime1 TIME DEFAULT TIME(now());
it works on MariaDB, but it doesnt work on Mysql.
On mysql it produces Error -
[42000][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 '(now())' at line 1
Can someone tell me why? whats wrong here? I thought that this should be the same.
In MySQL the expression used in DEFAULT field attribute must be wrapped into the parenthesis:
ALTER TABLE products ADD openTime1 TIME DEFAULT (TIME(now()));
db<>fiddle here

MySQL syntax issue on database and table

What is wrong with my syntax?
I'm trying to update several databases in one shot:
update `db_name1`.`db_table` SET `cc_number_enc` = NULL
update `db_name2`.`db_table` SET `cc_number_enc` = NULL
update `db_name3`.`db_table` SET `cc_number_enc` = NULL
and I'm getting query syntax error in phpmyadmin
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 'cc_number_enc = NULL' at line 1
UPDATE
I've rewritten the same query simply by copying & pasting, and now getting the following:
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 'cc_number_enc = NULL' at line 1
Question, is it matter from which database I'm running the query from in phpmyadmin?
try puting semicolon after query. It can happen because of multiple reasons.
references http://www.inmotionhosting.com/support/website/database-troubleshooting/error-1064
MySQl Error #1064
MySQL Nested Queries with Joins
Looks like when copying, your backticks are changed to some other symbol, which looks like backtick, but actually, it is not.
Try again without quotes, or manually put backticks in place.

Enter Image URL (http://...) in mysql table

I tried using this in mysql:
INSERT INTO users (url) VALUES (http://31.media.tumblr.com/tumblr_ljtc6i9GPA1qbq4v6o1_400.gif) WHERE id='15';
But it gives this error:
"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 '://31.media.tumblr.com/tumblr_ljtc6i9GPA1qbq4v6o1_400.gif) WHERE id='15'' at line 1"
Can anyone tell me what is the reason and how to store the url in the table?
INSERT INTO statements don't have a WHERE clause. If you are wanting to use a WHERE clause you are just UPDATEing a row.
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;

SQL View from another table

I am trying to create a VIEW from one databse to another, they are both on the same SQL server.
CREATE VIEW mdl_role_assignments
AS
SELECT *
FROM the-db.mdl_role_assignments
Any ideas as to why i cant get this working.
I think its the hyphen in 'the-db', but i need to use this the db is already used.
(Was named by someone else, i would use underscore)
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 '-db.mdl_role_assignments' at line 4
You could try square brackets to reference complicated names in SQL e.g.
CREATE VIEW mdl_role_assignments
AS
SELECT *
FROM [the-db].mdl_role_assignments;
or backticks
CREATE VIEW mdl_role_assignments
AS
SELECT *
FROM `the-db`.`mdl_role_assignments`;