I am trying to merge two separate queries that work into one query that would also work.
I have several tables and need some data from all of them.
I have a table of tasks
(this is just the tasks themselves, some may have been completed).
Each tasks has one or more rewards for completing the task, so I also have a reward_categories
table which contains all the possibly rewards, and a reward_task_categories
table which contains which rewards are associated to which task.
I have another table which inner joins to the tasks
table which contains a list of todo_tasks
and who needs to complete them.
I have a table of characters
some of these will be completing the todo_tasks
.
So I need all the fields from then todo_tasks
table, just the name of the task from the tasks
table, the name of the character to complete the task from the characters
table and finally the stumbling block one reward that the character would get from completing the task.
I'm basically trying to merge these two queries.
SELECT `tt`.`id`, `t`.`task_name` FROM `todo_tasks` `tt`
INNER JOIN `tasks` `t` ON `tt`.`task_id` = `t`.`id`;
SELECT `category_id`, `category_name` FROM `reward_task_categories` `rtc`
INNER JOIN `reward_categories` `rc` ON `rtc`.`category_id` = `rc`.`id`
WHERE `task_id` = 1 ORDER BY `rtc`.`category_id` ASC LIMIT 1;
I have tried these queries with no success so far.
SELECT `tt`.`id`, `t`.`task_name`, `rc`.*, `tt`.`character_id`,
`c`.`character_name`, `c`.`character_priority` FROM `todo_tasks` `tt`
INNER JOIN `tasks` `t` ON `tt`.`task_id` = `t`.`id`
INNER JOIN `characters` `c` ON `tt`.`character_id` = `c`.`id`
INNER JOIN
(
SELECT * FROM `reward_task_categories` `rtci`
INNER JOIN `reward_categories` `rc` ON `rtci`.`category_id` = `rc`.`id`
WHERE `task_id` = `tt`.`task_id` ORDER BY `rtci`.`category_id` ASC
LIMIT 1
) `rtc` ON `t`.`task_id` = `rtc`.`task_id`
LIMIT 1;
If I take out the sub query the above works, albeit missing some data I need. As it is it gives me the error Unknown table 'rc'
I guess this is from the INNER JOIN sub query.
Based on @Barmar comment from below I believe they are saying I need to do something like this, this query however still gives me the error of Unknown table 'rc'
.
SELECT `tt`.`id`, `t`.`task_name`, `rc`.*, `tt`.`character_id`,
`c`.`character_name`, `c`.`character_priority` FROM `todo_tasks` `tt`
INNER JOIN `tasks` `t` ON `tt`.`task_id` = `t`.`id`
INNER JOIN `characters` `c` ON `tt`.`character_id` = `c`.`id`
INNER JOIN
(
SELECT * FROM `reward_task_categories` `rtci`
INNER JOIN `reward_categories` `rc` ON `rtci`.`category_id` = `rc`.`id`
WHERE `task_id` = `tt`.`task_id` GROUP BY `rtci`.`task_id`
ORDER BY `rtci`.`category_id` ASC LIMIT 1
) `rtc` ON `t`.`task_id` = `rtc`.`task_id`
LIMIT 1;
I've made some progress in that it now seems to work without any errors, the only thing is that when a task has multiple rewards it repeats the task x times for each reward, I only want it to return the first reward it finds based on the order by condition.
SELECT `tt`.`id`, `t`.`task_name`, `rtc`.`category_name`, `tt`.`character_id`,
`c`.`character_name`, `c`.`character_priority` FROM `todo_tasks` `tt`
INNER JOIN `tasks` `t` ON `tt`.`task_id` = `t`.`id`
INNER JOIN `characters` `c` ON `tt`.`character_id` = `c`.`id`
LEFT JOIN
(
SELECT * FROM `reward_task_categories` `rtci`
INNER JOIN `reward_categories` `rc` ON `rtci`.`category_id` = `rc`.`id`
) `rtc` ON `tt`.`task_id` = `rtc`.`task_id`
LIMIT 1;