gpt4 book ai didi

php mysql join 查询别名

转载 作者:行者123 更新时间:2023-11-29 10:15:24 26 4
gpt4 key购买 nike

两张 table 。我想从设置表中选择 2 个名称。我认为连接表对于这个简单的要求来说太过分了,请告诉我。

错误号:1066

不唯一的表/别名:“设置”

SELECT `parameter`.*, `settings`.`name` as `unitname`, `settings`.`name` as `sourcename` 
FROM `parameter`
INNER JOIN `settings` ON `parameter`.`unit_id` = `settings`.`id`
INNER JOIN `settings` ON `parameter`.`source_id` = `settings`.`id`
WHERE `parameter`.`isdeleted` =0
ORDER BY `parameter`.`id` DESC

最佳答案

您将同一个表连接两次,因此当您引用该表时,查询引擎无法知道您指的是哪个连接表。给表起别名(在查询中给它们临时名称)以保持它们可单独识别:

SELECT
`parameter`.*,
`unit_settings`.`name` as `unitname`,
`source_settings`.`name` as `sourcename`
FROM
`parameter`
INNER JOIN `settings` AS `unit_settings` ON `parameter`.`unit_id` = `unit_settings`.`id`
INNER JOIN `settings` AS `source_settings` ON `parameter`.`source_id` = `source_settings`.`id`
WHERE
`parameter`.`isdeleted` = 0
ORDER BY
`parameter`.`id` DESC

关于php mysql join 查询别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50188220/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com