Implementing One to One and One to Many Query together in SQL - php

I have the following tables (Have only included necessary columns) which I'm looking to perform a query on. Some notes:
1) Table 1 has a one-one relationship with Tables 2 & 3, with the common key being menu_id.
Tables 4-6 are a little more complicated:
2) Table 1 has a one to many with table 4, so one menu can have many categories.
3) Table 4 has a one to many with table 5, so one category (Out of multiple above) can have many items.
4) Table 5 has again a one to many with table 6, so one item can have many subitems.
Table 1: -- user_menu --
menu_id
Table 2: -- menu_details --
menu_id
Table 3: -- menu_times --
menu_id
Table 4: -- menu_categories --
menu_category_id
menu_id
Table 5: -- menu_items --
menu_item_id
menu_category_id
Table 6: -- menu_subitems--
menu_subitem_id
menu_item_id
Question: I need a sql query which will fetch the all associated data from the tables (All columns, I haven't included them here, just the primary key rows) for a specific menu_id on table 1. That seems simple enough, but I'm struggling a little on the one to many part and getting that data in the same query.
I'm expecting something like this. Where menu_id = 2, it will fetch the associated row data (all columns). I've shortened the key names to fit.
user_menu | menu_details | menu_times | menu_categories | menu_items | menu_subitems
-------------------------------------------------------------------------------------
menu_id=2 - menu_id=2 - menu_id=2 -< cat_id=1 -< item_id=1 subitem_id=1
cat_id=2 -< item_id=2 -< subitem_id=2
cat_id=3 item_id=3 subitem_id=3
item_id=4 -< subitem_id=4
item_id=5 -< subitem_id=5
subitem_id=6
subitem_id=7
(I've shortened above. The proper names are in the first list, so please use those).
The ID's refer to the primary key rows I need the associated column data for. To clarify again If I were to query for menu_id = 2, I will get the menu_id = 2 row from table 2 & 3. I will also get the categories WHERE menu_id = 2 in the menu_categories table, the item_id rows where menu_category_id = x in the menu_item table, and the subitem rows where item_id = x from the menu_item table, in the subitem table.
I plan on copying the result to an array and passing that back. Just need the query bit (I'm assuming join but I'm not sure which is relevant here).
Thanks!

Yes, you can just use join, joining them using the key references should output it like that:
select A.menu_id, B.menu_id, C.menu_id, D.menu_category_id, E.menu_item_id,
F.menu_subitem_id
from user_menu A
join menu_details B on A.menuid = B.menuid
join menu_times C on B.menuid = C.menuid
join menu_categories D on C.menuid = D.menuid
join menu_items E on D.menu_category_id = E.menu_category_id
join menu_subitems F on E.menu_item_id = F.menu_item_id
where A.menu_id = 2

Related

How to use find_in_set with join in codeigniter

I have 4 tables, and I want to fetch data from all the tables, I can do this by fetching data one by one from each table but I want to do it by using JOIN.
Main Table (which contains ids of other table data)
2, 3, 4 tables
now I want to fetch these fields.
from Table 1 (Main Table) - franchise_name, franchise_phone
from Table 2 (State Table) - state_name
from Table 3 (City Table) - city_name
from Table 4 (Area Table) - area_name
the first table contains ids of everything which I need to fetch from other tables.
but area_id in the main table is inserted as a string in the same row separated by (,) in field franchise_area.
I tried using FIND_IN_SET but did not work.
Read all 1-1 referred data using Join, cycle through data exploding area_id column
area_id -------> ($value = explode($row->area_id, ',')
then read data from database and insert into response array (or object).
Of course all of this operation must be done into the model...

UPDATE multiple MySQL rows with different values

Note: I realize this may be confusing taking about tables and columns below so here is a simplified version of the two tables I mention:
Table "categories" has columns: id, type
Table "entries" has columns: id, categories, ...
I have a MySQL table named entries where one of the columns, named categories, is a string of pipe separated indices. For example, "|1|2|3|" might be a possible value and "|1|3|4|" could be another. The numbers represent indices of the table categories.
I'm writing an admin script that allows a new row to be inserted into the categories table and when doing this, the appropriate rows of the table entires should have the categories column updated. For example, if a newly inserted row of the categories table has index 5 then "5|" should be concatenated to each appropriate column categories of the entries table.
I realize that I could could use UPDATE per appropriate entries row when adding a new category but am wondering if there is a better way to go about this. In case this is not clear I know that this is an option but want to know if this can be made into one statement (pseudo-code):
foreach ($entriesToUpdate as $currEntry)
"UPDATE entires SET categories='".$currValue."|".$newIndex."' WHERE id=".$currId;
This can be done with an expression-based update:
UPDATE entries SET categories=CONCAT(categories, "5|") WHERE id IN (1,2,3,4,...)
(5| instead of 5| from your example, since your examples seem to show that the existing value will start and end with |s already.)
That said, you'd probably be better off with a database schema that stores a mapping of entries to categories in a separate many-to-many table. For example:
categories:
id | type | ...
---------------
1 ...
2 ...
3 ...
entries:
id | ...
---------
100 ...
101 ...
102 ...
entries_to_categories:
entry | category
----------------
100 1
100 2
101 3
Then you can use JOINs to retrieve the set of categories if desired, or check if something is in a category. In this case, entry 100 is in categories 1 and 2, entry 101 is in category 3, and entry 102 is in no categories.

How to get single row from a query involving two tables. 2 Foreign Keys are repeated in one entry of child table

I have two tables
1. tbl_clubs
2. tbl_match_schedule
tbl_club has fields;
fld_id | fld_club_name
tbl_match_schedule has fields;
fld_id | fld_club_id_one | fld_club_id_two | fld_match_time.
Now the flow is that admin will
1. go to the add new match schedule.
2. Select the name of club from drop down 1
3. Select the name of the 2nd club (playing against) from drop down 2
4. Give Match time and save.
Now the problem is that I am unable to select the data properly. I need one row, but it
gives me two rows when I put the following query
SELECT
ms.fld_id, ms.fld_id_club_one , ms.fld_id_club_two , ms.fld_match_time, c.fld_club_name, c.fld_id
FROM
tbl_match_schedule as ms, tbl_club as c
WHERE
c.fld_id = ms.fld_id_club_one OR c.fld_id = ms.fld_id_club_two
Please help me, I want something in this form "club name one" vs "club name two" at "19:00"
I am using mysql
If i understand correctly this should do:
SELECT c1.fld_club_name AS 'club_name_one', c2.fld_club_name AS 'club_name_two', s.fld_match_time AS 'at' FROM tbl_match_schedule s
LEFT JOIN tbl_clubs c1 ON s.fld_club_id_one = c1.fld_id
LEFT JOIN tbl_clubs c2 ON s.fld_club_id_two = c2.fld_id
Of course you can add a WHERE or ORDER clause after this to suit your further needs as long as you use the c1. and c2. shorthands when you refer to the clubs as the full table name would be ambiguous.

codeigniter db->select handle mysql joins automaticly

table
ID|owner_id|work_id|lorem|etc|
1 |00123 | 00213 |XXXXX|XXX|
2 |00124 | 00213 |XXXXX|XXX|
owner_id (fk) owners.id (owners[id,name,etc])
work_id (fk) work.id (work[id,name,etc])
question is can I set codeigniter that when I
select(table.*,work.name as work,owners.name as owner) from table
it automatically handle joins since that table already contain the fk-ref ? or I must include join('owner','owner.id=table.owner_id) ?
actually all what I want is that when I select a table that contains a fk this fk column is replaced with one column from ref row by just passing the column name on ref table without having to worry about creating a specific function in my module for that each query.
My current solution:
was to create a view for each table that contain a relation and replace all fk columns with desired ref value, but since i have 6 tables 5 of them with fk,i now have 6 tables and 5 view (11 tables)in db which is really kind confusing for me, so any smarter way to do this ?
I think you are making some confusion on what FK is and what it does within a table.
FK constraint grants data integrity when it's present and relates data within tables. It doesn't join anything.
If you want to select records across related tables, you either use a
SELECT * FROM table1,table2 WHERE table1.K1 = table2.FK1
or
SELECT * FROM table1 JOIN table2 ON table1.K1 = table2.FK1
AND YES, you need to tell CodeIgniter to do those queries

Show each number separated after comma in php

I have two tables
category
id | category
------
1 | One
2 | Two
etc
second one is
mp_photos
id|name|test|CategoryID
1 |name|test|1,2
So now i need to fetch data from mp_photos row CategoryID (1,2) every categoryid is seppareted by comma and link every number with Category name in first table
Best option would be to restructure your database and create another table with id_mp_photos and id_category and use joins.
If you cannot do it there are two options (both are slow):
use explode() in php
use MySQL function FIND_IN_SET(), for example:
SELECT mp_photos.name, mp_photos.test, category.category
FROM mp_photos JOIN category ON FIND_IN_SET(category.id, mp_photos.CategoryID) > 0

Categories