我需要使用这个php查询:
INSERT INTO table1(id,picture) SELECT id, $var.picture from table2
$var在哪里"http://example.com/images/".
示例(之前):
ID = 1
picture = 'flower.jpg'
示例(之后):
ID = 1
picture = 'http://example.com/images/flower.jpg'
我如何加入SELECT$var和图片(逗号无效)。。。
如果我正确理解你的问题,这应该会奏效:
INSERT INTO table1(id,picture) SELECT id, CONCAT($var, picture) FROM table2
您可以使用concat($var, '.picture')
。
但是,你不应该那样做。相反,您应该将$var
值作为参数传递到查询中。我会在应用程序端连接该值,但也可以将concat()
与参数一起使用。
您可以在php端执行它。只需使用sprintf():安装一个临时变量,该变量将包含格式正确的列
// Get the data
$id = 1;
$var = 'http://example.com/images/';
$picture = 'flower.jpg';
$column = sprintf('%s%s', $var, $picture);
// Set the query
$query = sprintf(' INSERT INTO table1 (id, picture)
SELECT %s, %s
FROM table2;', $id, $column);