MySQL - 无法运行查询
Posted
技术标签:
【中文标题】MySQL - 无法运行查询【英文标题】:MySQL - Trouble to run a query 【发布时间】:2020-09-04 05:42:03 【问题描述】:我有一个查询,我选择了一些列,然后我选择了left join
。我在mysql Workbench 使用MariaDB。
我尝试运行的查询
sequelize.query(`SELECT
distinct av.idAnnouncementVehicles as id,
av.price,
CONCAT("https://autoparanaiba.s3-sa-east-1.amazonaws.com/" , SUBSTRING_INDEX(ap.image, '.', 1) , '-355x180.' , SUBSTRING_INDEX(ap.image, '.', -1)) as image,
am.worked_hours,
am.year AS ano,
md.description AS modelo,
bn.description AS marca,
fu.description AS combustivel,
pw.description AS potência,
tc.description AS tração
FROM announcement_vehicles AS av
left join announcemment_photos AS ap on ap.annoucements_id = av.idAnnouncementVehicles
left join persons AS pe on pe.id = av.personId
left join agricultural_machine AS am on am.agricultural_machine_id = av.itemId
left join itens AS it on it.id = av.itemId
left join traction AS tc on tc.id = trac.traction_id
left join power AS pw on pw.id = trac.power_id
left join fuel AS fu on fu.id = trac.fuel_id
where it.type=$type and (av.plans_id IS NOT NULL or pe.type = 'jud')
group by av.idAnnouncementVehicles order by av.plans_id, av.idAnnouncementVehicles desc limit 8`, type: sequelize.QueryTypes.SELECT )
.then((result) =>
return res.json( success: true, result: result )
).catch((err) =>
return res.status(400).json(err)
)
此查询正在向我返回此错误。但是我在代码中有一个类似的查询不会抛出这个错误。
错误
"name": "SequelizeDatabaseError",
"parent":
"code": "ER_BAD_FIELD_ERROR",
"errno": 1054,
"sqlState": "42S22",
"sqlMessage": "Unknown column 'md.description' in 'field list'",
我有类似的查询
sequelize.query(`SELECT
distinct av.idAnnouncementVehicles as id,
av.price,
CONCAT("https://autoparanaiba.s3-sa-east-1.amazonaws.com/" , SUBSTRING_INDEX(ap.image, '.', 1) , '-355x180.' , SUBSTRING_INDEX(ap.image, '.', -1)) as image,
ve.mileage,
fi.marca,
fi.name,
fi.ano,
fi.ano_modelo as modelo,
fu.description as combustivel
FROM announcement_vehicles as av
left join announcemment_photos as ap on ap.annoucements_id = av.idAnnouncementVehicles
left join persons as pe on pe.id = av.personId
left join vehicles as ve on ve.item_id = av.itemId
left join itens as it on it.id = av.itemId
left join fipe as fi on fi.id = ve.fipe_id
left join fuel as fu on fu.id = ve.fuel_id
left join color as co on co.id = ve.color_id
where it.type=$type and (av.plans_id IS NOT NULL or pe.type = 'jud')
group by av.idAnnouncementVehicles order by av.plans_id, av.idAnnouncementVehicles desc limit 8`, type: sequelize.QueryTypes.SELECT )
.then((result) =>
return res.json( success: true, result: result )
).catch((err) =>
return res.status(400).json(err)
)
【问题讨论】:
在您的查询中没有别名为 md 的表 - 因此未知列 md.description 您也没有别名为bn
的表。而且,大概您知道SELECT DISTINCT
适用于结果集的所有列。
@P.Salmon 我如何使用别名?
@O.Jones 我能做些什么来解决这个问题?
您在整个查询中使用表别名,因此您对此的评论令人惊讶。如果您真的不知道别名是什么,那么您应该在别处研究该主题。您的查询无法修复,因为我们不知道它应该做什么,并且从非工作代码进行逆向工程是一场无聊的游戏。
【参考方案1】:
我解决了这个查询的问题
sequelize.query(`SELECT
distinct av.idAnnouncementVehicles as id,
av.price,
CONCAT("https://autoparanaiba.s3-sa-east-1.amazonaws.com/" , SUBSTRING_INDEX(ap.image, '.', 1) , '-355x180.' , SUBSTRING_INDEX(ap.image, '.', -1)) as image,
am.worked_hours,
am.year AS ano,
md.description AS modelo,
bn.description AS marca,
fu.description AS combustivel,
pw.power AS potência,
tc.description AS tração
FROM announcement_vehicles AS av
left join announcemment_photos AS ap on ap.annoucements_id = av.idAnnouncementVehicles
left join persons AS pe on pe.id = av.personId
left join agricultural_machine AS am on am.agricultural_machine_id = av.itemId
left join model_agricultural_machine as md on md.id = am.model_agricultural_machine_id
left join brand_agricultural_machine as bn on bn.id = am.brand_agricultural_machine_id
left join tractor as trc on trc.tractor_id = am.agricultural_machine_id
left join power as pw on pw.id = trc.power_id
left join itens AS it on it.id = av.itemId
left join traction AS tc on tc.id = trc.traction_id
left join fuel AS fu on fu.id = trc.fuel_id
where it.type=$type and (av.plans_id IS NOT NULL or pe.type = 'jud')
group by av.idAnnouncementVehicles order by av.plans_id, av.idAnnouncementVehicles desc limit 8`, type: sequelize.QueryTypes.SELECT )
.then((result) =>
return res.json( success: true, result: result )
).catch((err) =>
return res.status(400).json(err)
)
【讨论】:
以上是关于MySQL - 无法运行查询的主要内容,如果未能解决你的问题,请参考以下文章