如何将通配符 % 添加到“specifications.sn”?
SELECT `products2`.`type`,
`products2`.`sn`,
`products2`.`lap`,
`specifications`.`year`
FROM `products2`
INNER JOIN `specifications`
ON `products2`.`sn` LIKE `specifications`.`sn`
WHERE `products2`.`id` = ? LIMIT 1
编辑:
添加了当前查询
- 删除了反引号
添加了您的 CONCAT
SELECT products2.type, products2.sn, products2.lap, specifications.year FROM products2 INNER JOIN specifications ON products2.sn LIKE CONCAT('%', specifications.sn, '%') WHERE products2.id = ? LIMIT 1
替代查询 - 与上面的代码相同的错误
- 删除了反引号
- 删除了 INNER JOIN
在 WHERE 语句中添加 LIKE
SELECT products2.type, products2.sn, products2.lap, specifications.year FROM products2, specifications WHERE products2.id = ? AND products2.sn LIKE '%' + specifications.sn + '%' LIMIT 1
编辑2
PHP 代码
if ($stmt = $mysqli->prepare('SELECT products2.type,
products2.sn,
products2.lap,
specifications.year
FROM products2
INNER JOIN specifications
ON products2.sn LIKE CONCAT('%', specifications.sn, '%')
WHERE products2.id = ? LIMIT 1')) {
$stmt->bind_param('i', $id);
$id = $_GET['id'];
$stmt->execute();
$stmt->bind_result($type, $sn, $lap, $year);
$stmt->fetch();
echo $type . '<br/>';
echo $sn . '<br/>';
echo $lap . '<br/>';
echo $year . '<br/>';
$stmt->close();
} else {
echo $mysqli->error;
}