I am looking for a way to convert the result of a query to a string.
The query can be
DESC entries;
or
SHOW COLUMNS FROM entires;
For example, both yield the same result if I execute them in the CLI which is:
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(45) | YES | | NULL | |
| content | text | YES | | NULL | |
| time | int(11) | YES | | NULL | |
+---------+-------------+------+-----+---------+----------------+
However, I need this table as one string. It is NOT an option for me to get his information out of the information_schema.columns table because this information is not present there. It is further necessary to achieve this on the same query. Using PHP or another language is also not an option.
I have tried various things but none of them have been successful so far.
These queries all resulted in an error:
SELECT group_concat(Field) FROM (SHOW COLUMNS FROM entries);
SELECT group_concat(SHOW COLUMNS FROM entries);
SELECT group_concat(SHOW COLUMNS FROM entries LIMIT 1);
SELECT Field from (SHOW COLUMNS FROM entries);
SELECT 1 from (SHOW COLUMNS FROM entries);
SELECT group_concat(SHOW COLUMNS FROM entries);
SELECT group_concat(SHOW COLUMNS) FROM entries;
I have tried the same with desc entries; but with the same result.
I always get an error like this:
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 'show columns
from entries)' at line 1
Neither Google nor the manual could tell me how to achieve this, maybe I was looking for the wrong phrases. Any help is highly appreciated.
Related
I have the following table (mariadb 10.4) called p:
+----------------+----------------------------------------------------------------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+----------------------------------------------------------------------------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| description | text | YES | | NULL | |
| url | text | YES | | NULL | |
| source | enum('source_a','source_b','source_c','source_d','source_e') | YES | | NULL | |
I currently have a couple of million rows on this table with the sources a, b, c, and d. Just recently we applied a migration to add source_e and we started getting the error ERROR 1265 (01000): Data truncated for column 'source' at row 1 when trying to inset a row with the source_e. The used command that yields the error is the following:
INSERT INTO p (description, url, `source`) VALUES ('test', 'https://google.com.br', 'source_e');
Insertions with any of the other sources are still working.
The behavior changes when editing a row that is already on the db, the error is not shown:
UPDATE `p` SET `source`='source_e' WHERE `id`='3';
Yields:
Query OK, 1 rows affected (0.001 sec)
Is there a way to debug this scenario? I've tried changing the log level of the db to get a better insight on the problem (SET GLOBAL log_warnings=3;) but the error message did not change.
I also tried changing the source_e name to source_e_, the error persisted.
Btw, i did change the name of the fields to comply with company policies.
It turns out it was my bad. We happen to have a trigger on insertions of this table that feeds a materialized view kind of table. All I had to do was add 'source_e' to the source field on the other table.
I cannot change databases column
My Env
MacOS Mojave, MySQL Server version: 10.1.39-MariaDB Source distribution
why
Making a CRUD app, but I want to change table column,
from text to desc, so I searched and used alter
command, but right SQL command returns error messages.
My table
MariaDB [cake_cms]> describe interns;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| email | varchar(255) | NO | | NULL | |
| name | varchar(64) | NO | | NULL | |
| text | varchar(255) | NO | | NULL | |
| location | varchar(64) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
MariaDB [cake_cms]> Alter Table interns Rename Column text to desc;
ERROR 1064 (42000): 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 'Column text to desc' at line 1
Refered
https://www.dbonline.jp/mysql/table/index18.html
says to use
ALTER TABLE table_name
CHANGE COLUMN old_name TO new_name;
Rename a column in MySQL
This site says:
ALTER TABLE tableName RENAME COLUMN "oldcolname" TO "newcolname" datatype(length);
So I write
alter table interns rename column "name" to "newname" varchar(255);
But returned syntax error message....
I do not know what to do. Please help me!
desc is a sql command so you can't name your table like this
There's something very very wrong with Doctrine/MySQL or I'm just completly dumb. I'm trying to execute a simple update query on Question entity:
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| form_id | int(11) | YES | MUL | NULL | |
| name | varchar(255) | NO | | NULL | |
| question_type | varchar(255) | NO | | NULL | |
| validation | longtext | YES | | NULL | |
| required | tinyint(1) | YES | | NULL | |
| order | int(11) | NO | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
With:
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('order', IntegerType::class)
->add('name', TextType::class)
->add('questionType', TextType::class)
->add('validation', TextType::class)
->add('required', CheckboxType::class)
;
}
and
...
$em->persist($question);
...
I'm getting this error:
Uncaught PHP Exception Doctrine\DBAL\Exception\SyntaxErrorException: "An exception occurred while executing 'UPDATE questions SET order = ? WHERE id = ?' with params [1, 12]
Every other field validates with no problem!
When I'm trying to run the same query in the console, this is the result:
mysql> UPDATE questions SET order=1 WHERE id=14;
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 'order=1 WHERE id=14' at line 1
When I run the same query in Workbench, everything is ok:
UPDATE `questions` SET `order`='2' WHERE `id`='7';
Finally, updating another column in this table (also integer!), like so:
update questions set form_id=5 where id=7;
Goes as expected.
What is going on?
EDIT:
To simplify, why this works:
UPDATE questions SET form_id=5 WHERE id=7;
And this doesn't:
UPDATE questions SET order=3 WHERE id=7;
order is a reserved word for Mysql
That's why it runs when (escaped with backticks)
It goes without saying that choosing another name would be safer
See this answer for more informations
I am changing a column from text to varchar column.
+------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+-------+
| ID | bigint(19) | NO | PRI | NULL | |
| STATUS | varchar(120) | YES | | NULL | |
| PRIORITY | varchar(120) | YES | | NULL | |
| DESCRIPTION | text | YES | | NULL | |
when i execute the below query,
alter table StatInfo modify column DESCRIPTION varchar(255) NULL;
It says
ERROR 1406 (22001): Data too long for column 'DESCRIPTION' at row 7
It doesn't truncates the value in the column and alters the table why?. where as in older version it works.
May be you need to check the sql mode, if it strict then it will show this error
When you change a data type using CHANGE or MODIFY, MySQL tries to
convert existing column values to the new type as well as possible.
Warning This conversion may result in alteration of data. For example,
if you shorten a string column, values may be truncated. To prevent
the operation from succeeding if conversions to the new data type
would result in loss of data, enable strict SQL mode before using
ALTER TABLE (see Section 5.1.6, “Server SQL Modes”).
http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
I suggest you to
1) Copy that table on a temporary table (through an insert-select)
2) Alter the original table
3) Restore the values in the original table using the same procedure described in 1)
This kind of sad but I've been at it a while and I just can't seem to figure this statement out, google searches turn up similar questions but I haven't successfully applied the solutions.
I have a table of music, and every time I insert a song into it(each row is a song) I want to insert the song into a table of clean music if it is flagged as clean. I'm using mysql.
use music;
CREATE TRIGGER cache_clean_music BEFORE INSERT ON music
FOR EACH ROW
if new.clean then
insert into clean_music values (new.artist, new.album, new.song, new.filename, new.clean);
end if;
The Error I get is
ERROR 1064 (42000) at line 3: 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 '' at line 4
and here is a description of the music table, the clean_music table is exactly the same
+----------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+----------------+
| artist | varchar(100) | YES | | NULL | |
| album | varchar(100) | YES | | NULL | |
| song | varchar(100) | YES | | NULL | |
| filename | varchar(100) | YES | | NULL | |
| clean | tinyint(1) | YES | | NULL | |
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
+----------+---------------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
If the two tables are identical (or almost), you probably do not need triggers (and all their mess) at all.
You may use a VIEW instead of a table (with duplicate data) for cache_clean_music:
CREATE VIEW cache_clean_music AS
SELECT artist
, album
, song
, filename ---- and possibly other fields you need
, id
FROM music
WHERE clean ;
Adding an index on music.clean would be a good idea in this case.
Does it help if you wrap a BEGIN...END around things?
CREATE TRIGGER ...
FOR EACH ROW BEGIN
IF ...
....
END IF;
END
The error - ERROR 1064 (42000) at line 3: 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 '' at line 4 - means that the values in some of your input params isn't correct, perhaps there is some mismatching single quote. Can you display your query or the value in the NEW. variables?
Also, once you have fixed that error, your query will also return another error that "the column count doesn't match value count". And that will be because your table has 6 columns but your INSERT has only 5. Mention the columns in your INSERT query and it should be fine, like:
insert into clean_music (artist, album, song, filename, clean) values (new.artist, new.album, new.song, new.filename, new.clean);
Your clue is this
" check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4"
Do you know what version of mysql server you are running?
Did you check the manual to make sure that the command you have written is allowed in that version?