How do I insert a record in SQL with reserved word “from”?

和自甴很熟 提交于 2020-01-30 13:11:45

问题


I am using CodeIgniter and I've noticed that one of our fields is named "from" because the table is for saving emails. So someone used "from" which is a reserved word from database.

I've used

$this->db->query("INSERT INTO email_setting (client_id, [from], created_by) VALUES (9251 , 'mjmsample@gmail.com', 1)")

I've also tried these codes

$this->db->query("INSERT INTO email_setting e (e.client_id, e.from, e.created_by) VALUES (9251 , 'mjmsample@gmail.com', 1)")

$this->db->query("INSERT INTO email_setting ('client_id', 'from', 'created_by') VALUES (9251 , 'mjmsample@gmail.com', 1)")

All of these but no luck, is there any workaround this?


回答1:


I don't understand why people never use backticks!

INSERT INTO `email_setting` `e` (`e`.`client_id`, `e`.`from`, `e`.`created_by`)



回答2:


 Use `` backtick symbol to use reserved keyword in mysql.It is a good practice to add backtick to all fields. 

USE `from` with backtick instead of 'from' with single quotes.

 $this->db->query("INSERT INTO email_setting (`client_id`, `from`, `created_by`) VALUES (9251 , 'mjmsample@gmail.com', 1)"



回答3:


You need to use back ticks for MySQL reserved keywords:

$this->db->query("INSERT INTO `email_setting` (`client_id`, `from`, `created_by`) VALUES (9251 , 'mjmsample@gmail.com', 1)")

It's a good habit to use back ticks for all MySQL-related names such as table names and columns.




回答4:


You need to use backtick(`) quotes instead of '



来源:https://stackoverflow.com/questions/37000314/how-do-i-insert-a-record-in-sql-with-reserved-word-from

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!