last day (27 days later) » 

5:58 AM
0
A: MySQL - How to handle case to use second foreign key if first foreign key is empty?

JamesUse case on Join, SELECT CONCAT('program:', program_pk) AS global_id, program_name AS name, NULL AS parent_global_id, 1 AS seq FROM program UNION ALL SELECT CONCAT('year:', year_pk) AS global_id, year_name AS name, CONCAT('program:', program_fk) AS parent_globa...

 
Again, Since you haven't said which column to be mapped directly i have added with the other one. You can now edit for your own.
 
There is a problem, as a learning_event (le) with week_fk = 0 and say, rotation_discipline_block_fk (rdb) = 22, the le shows in all weeks matching the rdb - see le (133, 'Antigen capture and processing', '', 0, 22), in db-fiddle.com/f/5fiF18h2xFeXyQ8cx5cb6K/0 What needs to happen is the le appear as a child of the rdb and not in any weeks
 
Would needs to happen is the le appear as a child of the rdb and not in any weeks explain this part. Coz when I was writing I saw that everything matches with rdb 22 and so I couldn't see any other output since no related columns were taken
 
'What needs to happen is the le appear as a child of the rdb and not in any weeks' if the week_fk = 0 and there is a corresponding value in rotation_discipline_block_fk. If this is the case, then the learning_event should show as a child of the rotation_discipline_block_name (rdb), not as child of any weeks that are related to the rdb (e.g. rdb with pk of 22 (Body defences)). I think the only learning_events identified in that table to weeks are those belonging to the parent rdb with pk = 22, though that will ultimately change.
See the required heirachical structure in my OP where learning_event changes to have parent of rotation_discipline_block, instead of being child of week for the rotation_discipline_block
 
Yeah I get it, so now I have showed you the method how to use case using join. Now you can alter right?
 
5:58 AM
The query is complex and I got a lot of help with this, but I think you would know better than I how to do this properly. Can you update your answer with working fiddle and I will accept gladly.
 
Currently I have limited access. I can look into this but it will be tomorrow.
 
OK, thanks very much
 
So clear me this, when le.week_fk=0 then which table and it's column to join and what would be your expected output. If you edit your question and add your current output part( the last block) and desired output which you need.
 
Updated OP...hope this is clear now.
 
How does the column result should be? For the sample which you have shared? global_id, parent_global_id
Just analyzed a bit more and shared my edit. Hope this would solve your problems.
 
5:58 AM
Thanks but I'm getting an error in the db_fiddle...can you please provide a link to a working fiddle?
 
@IlludiumPu36 added.
 
Sorry James, you are very close. However your query requires that there is an entry in table week with a rotation_discipline_block_fk that matches learning_event.rotation_discipline_block_fk, otherwise the learning event doesn't show in the results. An example in the fiddle in table learning_event is (135, 'Antigen recognition I - T cells', '', 0, 21)... this record doesn't show in the results because there is no week that matches in table week...we need to avoid table week altogether if learning_event.week_fk == 0 and there is a value in learning_event.rotation_disciplne_block_fk
Considering that I need this and how much work you have given to it, I will award a bounty of 50
Also, I don't think it needs GROUP BY global_id
 
Will add a detail explaination 👍 btw I have changed the column what to be displayed. If you try executing without group by there will be duplicate entries in the result
 
OK, just make sure you update the fiddle with (135, 'Antigen recognition I - T cells', '', 0, 21)...this record should display in your fiddle with parent rotation_discipline_block.rotation_discipline_block_pk = 21 as a test case. See db-fiddle.com/f/5fiF18h2xFeXyQ8cx5cb6K/2
 
But yesterday it was 133? now changed to 135.. alright will check. An example in the fiddle in table learning_event is (135, 'Antigen recognition I - T cells', '', 0, 21)... this record doesn't show in the results because there is no week that matches in table week...we need to avoid table week altogether if learning_event.week_fk == 0 and there is a value in learning_event.rotation_disciplne_block_fk.
First you said that when there is le.week=0 then it should join with rdb table but now it seems to be different, as i have already asked what kind of column result should be shown as a result for 135? global_id, parent_global_id
 
5:58 AM
Nothing has changed. Yes, le.week=0 then parent should be rdb table record. Except in your fiddle from yesterday, for an LE to show in results where week=0 and rdb_fk = some number, there needed to be a record in the week table with that rdb_fk. This should not be the case, that's all.
And yes, the example has changed, but it was just an example...
 
Okay, will share the updated one with your latest example.
 
About to go for the weekend and won't be able to check until Monday.
 
Completed you can check now
Hope this resolves your problem
Just made two separate query and union all the result based on values
 
6:15 AM
Great work indeed! Well done and thank you very much!
I have marked your answer as correct. I can't award the bounty of 50 though until 19 hours has passed (that is the message I just got when attempting to do that). I can do this tomorrow of Sunday, I won't forget.
...or Sunday
 
6:29 AM
Don't award me bounty wait till someone gives answer better than me.
 
 
17 hours later…
11:12 PM
I don't think that would happen as you have been the only person to offer any solution. Your solution works fine. So I will award the bounty to your answer in a few hours when it can be released.
 

  last day (27 days later) »