- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题描述
我将Users
表与Tests
表关联两次,一次在别名Maintainers
下。当我定义了Maintainers
belongsTo
关联时,没有返回任何结果。我需要弄清楚为什么第一个查询没有返回任何结果。
不起作用
SELECT
Tests.id AS `Tests__id`,
Tests.title AS `Tests__title`,
Tests.slug AS `Tests__slug`,
Tests.description AS `Tests__description`,
Tests.user_id AS `Tests__user_id`,
Tests.maintainer_id AS `Tests__maintainer_id`,
Tests.test_question_count_total AS `Tests__test_question_count_total`,
Tests.test_question_count_mature AS `Tests__test_question_count_mature`,
Tests.test_session_count_total AS `Tests__test_session_count_total`,
Tests.test_session_count_mature AS `Tests__test_session_count_mature`,
Tests.photo AS `Tests__photo`,
Tests.photo_dir AS `Tests__photo_dir`,
Tests.photo_size AS `Tests__photo_size`,
Tests.view_count AS `Tests__view_count`,
Tests.published AS `Tests__published`,
Tests.mature AS `Tests__mature`,
Tests.flagged AS `Tests__flagged`,
Tests.deleted AS `Tests__deleted`,
Tests.created AS `Tests__created`,
Tests.modified AS `Tests__modified`,
Users.id AS `Users__id`,
Users.first_name AS `Users__first_name`,
Users.last_name AS `Users__last_name`,
Users.username AS `Users__username`,
Users.email AS `Users__email`,
Users.password AS `Users__password`,
Users.token AS `Users__token`,
Users.photo AS `Users__photo`,
Users.photo_dir AS `Users__photo_dir`,
Users.photo_size AS `Users__photo_size`,
Users.bio AS `Users__bio`,
Users.is_admin AS `Users__is_admin`,
Users.created AS `Users__created`,
Users.modified AS `Users__modified`,
Maintainers.id AS `Maintainers__id`,
Maintainers.first_name AS `Maintainers__first_name`,
Maintainers.last_name AS `Maintainers__last_name`,
Maintainers.username AS `Maintainers__username`,
Maintainers.email AS `Maintainers__email`,
Maintainers.password AS `Maintainers__password`,
Maintainers.token AS `Maintainers__token`,
Maintainers.photo AS `Maintainers__photo`,
Maintainers.photo_dir AS `Maintainers__photo_dir`,
Maintainers.photo_size AS `Maintainers__photo_size`,
Maintainers.bio AS `Maintainers__bio`,
Maintainers.is_admin AS `Maintainers__is_admin`,
Maintainers.created AS `Maintainers__created`,
Maintainers.modified AS `Maintainers__modified`
FROM
tests Tests
INNER JOIN users Users ON Users.id = (Tests.user_id)
INNER JOIN users Maintainers ON Maintainers.id = (Tests.maintainer_id)
WHERE
(
Tests.slug = 'what-word-are-youquestion'
AND Tests.deleted IS NULL
)
LIMIT
1
作品
SELECT
Tests.id AS `Tests__id`,
Tests.title AS `Tests__title`,
Tests.slug AS `Tests__slug`,
Tests.description AS `Tests__description`,
Tests.user_id AS `Tests__user_id`,
Tests.maintainer_id AS `Tests__maintainer_id`,
Tests.test_question_count_total AS `Tests__test_question_count_total`,
Tests.test_question_count_mature AS `Tests__test_question_count_mature`,
Tests.test_session_count_total AS `Tests__test_session_count_total`,
Tests.test_session_count_mature AS `Tests__test_session_count_mature`,
Tests.photo AS `Tests__photo`,
Tests.photo_dir AS `Tests__photo_dir`,
Tests.photo_size AS `Tests__photo_size`,
Tests.view_count AS `Tests__view_count`,
Tests.published AS `Tests__published`,
Tests.mature AS `Tests__mature`,
Tests.flagged AS `Tests__flagged`,
Tests.deleted AS `Tests__deleted`,
Tests.created AS `Tests__created`,
Tests.modified AS `Tests__modified`,
Users.id AS `Users__id`,
Users.first_name AS `Users__first_name`,
Users.last_name AS `Users__last_name`,
Users.username AS `Users__username`,
Users.email AS `Users__email`,
Users.password AS `Users__password`,
Users.token AS `Users__token`,
Users.photo AS `Users__photo`,
Users.photo_dir AS `Users__photo_dir`,
Users.photo_size AS `Users__photo_size`,
Users.bio AS `Users__bio`,
Users.is_admin AS `Users__is_admin`,
Users.created AS `Users__created`,
Users.modified AS `Users__modified`
FROM
tests Tests
INNER JOIN users Users ON Users.id = (Tests.user_id)
WHERE
(
Tests.slug = 'what-word-are-youquestion'
AND Tests.deleted IS NULL
)
LIMIT
1
编辑:我被告知INNER JOIN
需要两个表中的数据,这可能是失败的原因,因为在tests
表中,maintainer_id
是null
。
最佳答案
我解决了这个问题,我使用的是CakePHP
框架。我最终从关联定义中删除了joinType
关联,并让框架决定连接类型。
编辑:我验证了有人告诉我的一个提示(请参阅操作编辑),这是因为我正在加载的特定记录中的maintainer_id
在NULL
上设置为LEFT JOIN
。ALEFT JOIN
要求两个表中都有数据。一旦我将一个maintainer_id
设置为一个已有的记录,一切都会有效。更好的选择是使用maintainer_id
,因为LEFT JOIN
并不总是会被填充。
不起作用
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Maintainers', [
'className' => 'Users',
'foreignKey' => 'maintainer_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Maintainers', [
'className' => 'Users',
'foreignKey' => 'maintainer_id'
]);
SELECT
Tests.id AS `Tests__id`,
Tests.title AS `Tests__title`,
Tests.slug AS `Tests__slug`,
Tests.description AS `Tests__description`,
Tests.user_id AS `Tests__user_id`,
Tests.maintainer_id AS `Tests__maintainer_id`,
Tests.test_question_count_total AS `Tests__test_question_count_total`,
Tests.test_question_count_mature AS `Tests__test_question_count_mature`,
Tests.test_session_count_total AS `Tests__test_session_count_total`,
Tests.test_session_count_mature AS `Tests__test_session_count_mature`,
Tests.photo AS `Tests__photo`,
Tests.photo_dir AS `Tests__photo_dir`,
Tests.photo_size AS `Tests__photo_size`,
Tests.view_count AS `Tests__view_count`,
Tests.published AS `Tests__published`,
Tests.mature AS `Tests__mature`,
Tests.flagged AS `Tests__flagged`,
Tests.deleted AS `Tests__deleted`,
Tests.created AS `Tests__created`,
Tests.modified AS `Tests__modified`,
Users.id AS `Users__id`,
Users.first_name AS `Users__first_name`,
Users.last_name AS `Users__last_name`,
Users.username AS `Users__username`,
Users.email AS `Users__email`,
Users.password AS `Users__password`,
Users.token AS `Users__token`,
Users.photo AS `Users__photo`,
Users.photo_dir AS `Users__photo_dir`,
Users.photo_size AS `Users__photo_size`,
Users.bio AS `Users__bio`,
Users.is_admin AS `Users__is_admin`,
Users.created AS `Users__created`,
Users.modified AS `Users__modified`,
Maintainers.id AS `Maintainers__id`,
Maintainers.first_name AS `Maintainers__first_name`,
Maintainers.last_name AS `Maintainers__last_name`,
Maintainers.username AS `Maintainers__username`,
Maintainers.email AS `Maintainers__email`,
Maintainers.password AS `Maintainers__password`,
Maintainers.token AS `Maintainers__token`,
Maintainers.photo AS `Maintainers__photo`,
Maintainers.photo_dir AS `Maintainers__photo_dir`,
Maintainers.photo_size AS `Maintainers__photo_size`,
Maintainers.bio AS `Maintainers__bio`,
Maintainers.is_admin AS `Maintainers__is_admin`,
Maintainers.created AS `Maintainers__created`,
Maintainers.modified AS `Maintainers__modified`
FROM
tests Tests
INNER JOIN users Users ON Users.id = (Tests.user_id)
LEFT JOIN users Maintainers ON Maintainers.id = (Tests.maintainer_id)
WHERE
(
Tests.slug = 'what-word-are-youquestion'
AND Tests.deleted IS NULL
)
LIMIT
1
关于mysql - 模型/表别名不起作用(INNER JOIN),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43025651/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!