How to make two tables Work together SQL - php

I have 2 tables
Table1&Table2
Table1 Contains this Field
1) Field_ID
2) Field_Machine_name
3) Field_Date
And
Table2 Contains this Field
1) Field_ID
2) Field_Machine_name
3) Field_Date_Add
How can I make Field_Machine_name In Table 1
work together with Table 2 in FieldMachine_name
I Want to add a record in Table 1 and automatically its added to Table 2
Until now I have to do it manually in Table 2 So that it Not worth
Thanks to anyone who can help

not much clear with question...think on writing trigger on table 1.So when insert happens in Table 1 it will insert record in table 2 too..

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...

Copying tables mysql

I am newbie to sql advance and i am trying to insert rows to one table by selecting from another one. But some bug is coming out.
Here is my problem
So i have two tables
table1 having id(autoincrement), names
table2 having id(autoincrement), names
Now at starting table1 is empty and table2 having 2 rows
1,'myself'
2,'yourself'
So the problem starts here
When i execute following query
Insert into table1 (names) select (names) from table2
So now both rows of table2 must be copied to table1
Ya its working fine.
But what about autoincrement value of id?
By till now table1 autoincrement id should store 3 since next row to be inserted should have id 3
But its not working like expected so table1 autoincrement id stores 4 i.e, 1(current id value)+2*(no of rows inserted)-1
So next time when i execute same query it inserts row with id 4. Skips id=3.
This is problem hope you all got what i am talking about.
Thanks for helping in advance.
Try this
INSERT INTO TABLE1 SELECT * FROM TABLE2
Go ahead an copy your data over, and then you can modify the starting auto_inc to whatever you choose.
ALTER TABLE table AUTO_INCREMENT = 1000;
Alter Table will take around 1 sec as it first creates shadow of the table then the query is processed and if the no. of rows of table goes above then it may take around 20-30 sec. so I dont think that alter is good answer for this problem. Hope someone will help you with insert command such as:
Insert into table1 set id = 1; etc...

How To Retrieve Columns from multiple tables

I have created two separate tables.
Table 1 (username,email,gender,age)
Table 2(taskname,description,createdon,duedate,workstatus).
Now I want to create a
table 3 (taskname,description,createdon,duedate,staffs,workstatus)
where staffs will show all the registered members' name and admin can select multiple users to allocate in a task.
I am a premature php learner.
Will anyone help me out to solve the problem,please ?
First of all, create two primary keys as user_id and task_id in table 1 and table 2 respectively. Once you are done with it, create table 3 with fields user_id and task_id as two foreign keys referencing table 1 and table 2 respectively and your requirement should get fulfilled.

Cakephp Find removed rows of two joined tables

I have table A and table B and they are both joined through a relationship. table B has a foreign key of table A's key. Table A is joined when the program runs because it is dynamic and could reside in any database. How would I find all the rows in table B that are no longer found in table A?
For ex.
Starting out we have
table A table B
4 4
3 3
5 5
later that evening
table A deleted some rows
table A table B
NULL 4
3 3
NULL 5
I want to get the keys 4,5 so that I can tell what was deleted.
Just use mysql where not in table. I would just use cakephp query method versus find. Here is a topic that already has been answered: Where not in table

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

Categories