我是node.js
的新手。我已成功连接MySQL数据库。我可以做简单的选择查询,但是当我尝试使用多个JOIN的查询时,它会在参数列表
app.get('/quotes', function (req, res) {
mc.query(`SELECT p.sku,pa.attribute_value as is_seen, pa1.attribute_value as rating,m.projects_unit_id as project_id,m.version,sp.total_sale_price as total_price,
p.published_on,im.image_url as image, im1.image_url as pdf_link FROM my_designs m INNER JOIN product p on m.sku=p.sku and p.is_deleted=0 and p.is_published=1
INNER JOIN supplier_product sp on sp.product_id=p.product_id and sp.is_deleted=0
LEFT JOIN images im on im.`key`=m.sku and im.image_type=36 and im.is_deleted=0
LEFT JOIN images im1 on im1.`key` = m.sku and im1.image_type = 15 and im1.is_deleted=0
LEFT JOIN product_attributes pa on pa.common_id = p.sku and pa.is_deleted = 0 and pa.attribute_name = 'is_seen'
LEFT JOIN product_attributes pa1 on pa1.common_id = p.sku and pa1.is_deleted = 0 and pa1.attribute_name = 'rating'
WHERE m.user_id=? and m.is_deleted=0 and m.projects_unit_id is not null ORDER BY p.published_on DESC`, function (error, results, fields) {
if (error) throw error;
return res.send({ error: false, data: JSON.stringify(results), message: 'quote list.' });
}); });
请告诉我哪里出错了。
答案 0 :(得分:0)
由于您使用的是模板文字
back-tick (``)
(重音符号)括起来
字符而不是双引号或单引号。(${expression})
。因此,您必须使用 im.${key}
所以,你的代码变成了,
app.get('/quotes', function (req, res) {
mc.query(`SELECT p.sku,pa.attribute_value as is_seen, pa1.attribute_value as rating,m.projects_unit_id as project_id,m.version,sp.total_sale_price as total_price,
p.published_on,im.image_url as image, im1.image_url as pdf_link FROM my_designs m INNER JOIN product p on m.sku=p.sku and p.is_deleted=0 and p.is_published=1
INNER JOIN supplier_product sp on sp.product_id=p.product_id and sp.is_deleted=0
LEFT JOIN images im on im.${key}=m.sku and im.image_type=36 and im.is_deleted=0
LEFT JOIN images im1 on im1.${key} = m.sku and im1.image_type = 15 and im1.is_deleted=0
LEFT JOIN product_attributes pa on pa.common_id = p.sku and pa.is_deleted = 0 and pa.attribute_name = 'is_seen'
LEFT JOIN product_attributes pa1 on pa1.common_id = p.sku and pa1.is_deleted = 0 and pa1.attribute_name = 'rating'
WHERE m.user_id=? and m.is_deleted=0 and m.projects_unit_id is not null ORDER BY p.published_on DESC`, function (error, results, fields) {
if (error) throw error;
return res.send({ error: false, data: JSON.stringify(results), message: 'Todos list.' });
}
);
});
<强> PS:强>
由于您有其他人知道的原因,键是一个reserved
字。
答案 1 :(得分:-1)
您正在尝试使用template literals。您需要在变量周围使用$
,这就是编写查询所需的方法,
`Select * from Table where key = ${key} and otherkey = ${otherkey}`