从另一个表插入具有一个值的数据Mysql [英] Inserting Data Mysql with One Value from another table

查看:112
本文介绍了从另一个表插入具有一个值的数据Mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题, 如何将数据插入到数据库表(mysql)中,该表从php发布表格中检索所有数据,并从另一个表中检索出这样的数据?

I have a question, how to insert data into database table (mysql) which all data retrieve from php post form and 1 from another table like this?

PHP获取:

$nama           = $_POST['nama'];
$alamat         = $_POST['alamat'];
$jenis_kelamin  = $_POST['jenis_kelamin'];
$shift          = $_POST['shift'];

插入查询:

    INSERT INTO test (`id`, `nama`, `alamat`, `jenis_kelamin`, `shift`) VALUES 
('$nama','$alamat','$jenis_kelamin', select id from shift where shift_name = '$shift')"

这是我尝试运行以下代码时的输出:

无效查询:您的SQL语法有错误;查看手册 对应于您的MariaDB服务器版本的正确语法 在行的'select from shift from id where shift_name ='pagi')'附近使用 1

Invalid query: 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 'select id from shift where shift_name = 'pagi')' at line 1

推荐答案

尝试使用insert into select from

Try using insert into select from

INSERT INTO test ( `nama`, `alamat`, `jenis_kelamin`, `shift`) 
   select '$nama','$alamat','$jenis_kelamin','$shift' from shift where shift_name = '$shift'

这篇关于从另一个表插入具有一个值的数据Mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆