Unable to perform the JOIN between branched folders in Zend - php

I am trying to fire a query which should give me result from multiple table.... I am doing JOIN in zend framework 1.10.0 ... but the problem is that i am having tables which are connected as a branch.
for example
Table 1 (T1 PK)
Table 2 (T2 PK, T1 FK)
Table 3 (T3 PK, T1 FK)
Table 4 (T4 PK, T2 FK)
Table 5 (T5 PK, T1 FK)
Now, i am able to join Table1, with Table2, Table3 & Table5, but the problem is what should i do with Table4, bcoz i want data from that table also... How can I make a query which can do Branch JOINS ... Working on this from 2 days ... Plz help me frnds ... Thanks In Advance

I haven't tested it but it should be ok.
When joining tables you must must specify the conditional expression of the join. So you just have to reference the right table when joining T4
$dbTable = new Aplication_Model_DbTable_T1();
$select = $dbTable->select()
->setIntegrityCheck(false)
->from(array("t1" => "table1"), array(t1 columns to fetch...))
->join(array("t2" => "table2"), "t2.t1 = t1.id",array(t2 cols to fetch))
->join(array("t3" => "table3"), "t3.t1 = t1.id",array(t3 cols to fetch))
->join(array("t4" => "table4"), "t4.t2 = t2.id",array(t4 cols to fetch))
->join(array("t4" => "table4"), "t5.t1 = t1.id",array(t4 cols to fetch))
->where(...);

Related

Split record from one column to two column in MysQL or PHP

I have a problem to split data from attendance machine. The data source after export from it like this:
id name att
1 John 01/04/2015 7:59:00
1 John 01/04/2015 17:44:00
1 John 02/04/2015 7:50:00
1 John 02/04/2015 18:14
Where record (in and out) from fingerprint time save in one column. And I want to split the data to be like this:
id name in out
1 John 01/04/2015 7:59:00 01/04/2015 17:44:00
1 John 02/04/2015 7:50:00 02/04/2015 18:14:00
How to split those record into 2 column in MySQL or PHP (maybe)? Thank you.
Assuming there is only one in/out per day, it's as simple as self-joining on the date and greater time.
select t1.id, t1.name, t1.att as `in`, t2.att as `out`
from table1 t1
inner join table1 t2
on date(t1.att) = date(t2.att) and t1.id = t2.id
and t2.att > t1.att
sql fiddle demo
If you want to create a brand new table with this data, so you can get rid of the import, you just need to use this query as the input to create table, like so:
create table new_table
as
select t1.id, t1.name, t1.att as `in`, t2.att as `out`
from table1 t1
inner join table1 t2
on date(t1.att) = date(t2.att) and t1.id = t2.id
and t2.att > t1.att
You could try this one.
SELECT a.userID,a.name,min(a.att)as `in`,max(a.att) as `out`
FROM
(
SELECT userID,name,str_to_date(att,'%m/%d/%Y%T') as att,str_to_date(att,'%m/%d/%Y') as attd
FROM attendance
) as a
GROUP BY a.userID,a.attd

trying to output the correct value from SQL query from comparing a different table

I'm very new with SQL and need assistance on how I can accomplish this task using the correct query.
I have 2 tables that I need to use. Table "TB1" has:
id Name
1 bob
2 blow
3 joe
table "TB2" has:
compid property
1 bob
2 blow
I am trying to get which compid is missing in "TB2" and insert it from "TB1"
the query I am doing is:
SELECT id, name from TB1, TB2 where id <> compid
what I get is 2 ouputs of Id 1, and 2, and 3 outputs from id 3. by using php:
for($i=0;$i <= mysql_num_rows($comp)-1; $i++)
{
echo mysql_result($comp, $i, 0)."<br>";
}
and I expected the ouput 3 but instead got this:
1
1
2
2
3
3
3
I understand its comparing all the rows within the table but is there a way to achieve what I am looking for?
Thanks for your time.
You are performing an implicit Cartesian JOIN which results in every row against every other row. You need to specify what attribute JOINs the two tables.
Using implicit syntax (not recommended):
SELECT id, name
FROM TB1, TB2
WHERE id <> compid
AND TB1.Name = TB2.property <-- Column join
Using explicit syntax:
SELECT id, name
FROM TB1
JOIN TB2
ON TB2.property = TB1.Name <-- Column join
WHERE id <> compid
To accomplish your goal you would need something along the lines of:
SELECT TB1.id, TB1.name
FROM TB1
LEFT JOIN TB2
ON TB2.property = TB1.Name
WHERE TB2.compid IS NULL
See it in action
It's best practice to always alias the columns you select to prevent ambiguity.
To select it you can do:
SELECT *
FROM TB1
WHERE id NOT IN (
SELECT compid
FROM TB2
);

A complicated mysql join

Ok, I have this first table which has, among other things:
table 1: id | depID (every id has one depID)
Then, I have a second table where I have table 2: userID | depID (where an userID is associated with multiple depIDs in separate rows. Also, I have table 3 with userID | rankID (where an userID is associated with one rankID).
I need to get all id and depID from table 1, and then to check, which userIDs of table 2 shares the same depID (table1.depID = table2.depID), and then, to check which of those userIDs from table 2 has rankID = $rID
Thanks guys.
I think this SQL should get you what you want, but I'm not 100% clear from the wording of the question:
SELECT table2.userID
FROM table1
JOIN table2
ON table1.depID = table2.depID
JOIN table3
ON table2.userID = table3.userID
AND table3.rankID = $rID;

how to edit/manage multi tiered conditional data in php

I have 4 tables in a mysql database that i need to add/edit/manage.
The difficulty, is that each one, is dependent on the one before it.
The way i have this setup on the user end, is you select an option from table 1.
You are then presented with the options in table 2, that have the first option's ID in their row.
Once you select option in table 2, you are taken to table 3, which generates its list where the rows contain the ID of your selection in table 2, and so on and so forth.
The problem is how to edit/manage this.
Obviously if you delete an option in table 1, all its subsequent child options will be invalid.
I just need an idea of how to manage a setup like this.
Without many details it's hard to comment on your exact situation, but here's a way to organize your MySQL database structure:
TABLE table1
INT id
... other data ...
TABLE table2
INT id
INT table1_id FOREIGN_KEY table1
... other data ...
TABLE table3
INT id
INT table2_id FOREIGN_KEY table2
... other data ...
TABLE table4
INT id
INT table3_id FOREIGN_KEY table3
... other data ...
And your url structure for your site could be:
/table1/add
/table2/add?table1_id=T1_ID
/table3/add?table2_id=T2_ID
/table4/add?table3_id=T3_ID
/table1/(edit,delete)/T1_ID
/table2/(edit,delete)/T2_ID
/table3/(edit,delete)/T3_ID
/table4/(edit,delete)/T4_ID
For adding to table2,3,4:
INSERT INTO table2 (data, table1_id) VALUES(data, T1_ID);
For deleting from table1:
DELETE FROM table1 WHERE id = T1_ID;
$t2_ids = SELECT id FROM table2 WHERE table1_id = T1_ID;
DELETE FROM table2 WHERE table1_id = T1_ID;
$t3_ids = SELECT id FROM table3 WHERE table2_id IN ($t2_ids);
DELETE FROM table3 WHERE table2_id = $t2_ids;
$t4_ids = SELECT id FROM table4 WHERE table3_id IN ($t3_ids);
DELETE FROM table4 WHERE table3_id = $t3_ids;
And so and so forth if you're deleting from the sub tables.
Additionally, if your data in each table isn't very different, you can use a single table to maintain the parent/child relationships
TABLE table
INT id
INT parent_id
... other data ...
Your URL structure won't change much, but your deleting pseudo-code can be optimized to use subselects on the table itself.
Multi-tiered data is most-often stored in with a parent:child relation field in a table.
id | name | parent_id
----------------------
1 | Mom | NULL
2 | Son | 1
Write a recursive function/query to traverse the relationships and any updates/inserts/deletes should be relatively simple.

Join tables - MySQL & PHP

I'm trying to join two tables. The first table has a list of 11 items which are 'site_names' with an auto id field of 'id'. The second table that I want to connect has an auto id field of 'desc_id' and another field of 'descriptions'. This second table currently has 3 rows of data that I want displayed only for id 1 in table 1.
So, I want to accomplish is to connect the first site in table one with an id of '1' to the entire second table.
I can't seem to figure out how connect only the first entry(id=1) in table 1 to all the rows in table 2 (tb.1->id->1 to tbl.2->desc_id->1,2,3).
I hope that made sense. Any help would be great. Thanks
Try:
select site_name, descriptions
from table_1
inner join table_2
on 1 = 1
where table_1.site_id = 1
This should join give you what you want.
OK - based on the comment, I'm guessing what you want is:
site1 | desc1 | desc2 | desc3
all on one row. This is a bit trickier - particularly if you want it to remain open to an arbitrary number of descriptions. For just 3 (or, really, any limited subset, but as the number goes up, it gets ugly), you could do:
select site_name, t2.desc, t3.desc, t4.desc
from table_1
inner join table_2 t2
on t2.desc_id = 1
inner join table_2 t3
on t3.desc_id = 2
inner join table_2 t4
on t4.desc_id = 3
where site_id = 1
This kind of stuff is highly irregular though. It seems to me like something about your schema is probably not quite right to generate this sort of requirement.
Here is the query:
<?php
$mysql = new mysqli('localhost', 'root', 'root') or die('counld not connect');
$result = $mysql->query("SELECT ajax_demo.explore.site_name, anthony1.property.descriptions FROM ajax_demo.explore INNER JOIN anthony1.property ON ajax_demo.explore.id = anthony1.property.desc_id") or die($mysql->error);
if($result)
{
while($row = $result->fetch_object())
{
$id = $row->id;
$siteName = $row->site_name;
$siteDescription = $row->site_description;
echo "$siteName";
echo "$siteDescription";
}
}
?>
I may be missing something here, but it sounds to me like you need to add a foreign key to the Site table. If I understand your question correctly, your tables should look something like this:
Site
- SiteID
- DescriptionID
- SiteName
Description
- DescriptionID
- Description
Then your query to get Sites and their associated Descriptions would look like this:
SELECT
s.SiteName,
d.Description
FROM
Site s INNER JOIN Description d
ON s.DescriptionID = d.DescriptionID
This table structure assumes that multiple Sites share single Descriptions (as per your posted question).

Categories