SQL Delete query INNER JOIN - php

I'm currently trying to delete a row from a mysql table using php. I have two very basic tables. 'worksheet' and 'worksheet_labour'. Worksheet contains a field called job_ID and it has a field called WS_ID. Worksheet_Labour has a field called WS_ID and various other fields. I'm trying to delete all of the information within worksheet_labour for a particular job_ID that can be found in worksheet. This is what I've attempted so far but haven't had any luck so far. Any help would be greatly appreciated. Thanks
if(isset($_GET["delete"]))
{
$SQL = "DELETE FROM worksheet_labour INNER JOIN worksheet ON worksheet_labour.WS_ID = worksheet.WS_ID WHERE job_ID = '1234'";
$resultset2 = mysql_query($SQL);
}

Give this a try:
$SQL = "DELETE wl FROM worksheet_labour wl INNER JOIN worksheet w ON wl.WS_ID = w.WS_ID WHERE wl.job_ID = '1234'";

Related

How to user SQL two table field.? PHP

Here I want to access two table field but I cant get success. Here is my little code. please check that. I want to access Analysis.Right and tabl3.right.
I am printing its with foreach loop. like $res['Right'] of Analysis and $res['right'] of tabl3. when I try to print this it's show me error
Undefind index Right.
any one can help me
$qry = "select Analysis.Q_Id, tabl3.Q_Id, Analysis.Right, tabl3.right from tabl3 INNER JOIN Analysis ON Analysis.Q_Id = tabl3.Q_Id where Analysis.Q_Id = 3";
please help..
you have tow column with right name related to different table so there is not a column name right but 'Analysis.Right ' or 'tabl3.right'
or you can assign an alias for set the column name equalt to Right where you need .. eg:
$qry = "select
Analysis.Q_Id
, tabl3.Q_Id
, Analysis.Right as Right
, tabl3.right as Right_t3
from tabl3
INNER JOIN Analysis ON Analysis.Q_Id = tabl3.Q_Id where Analysis.Q_Id = 3";
Your result set has columns with the same name. Give them different names:
select t3.Q_Id, a.Right as a_right, t3.right as t3_right
from tabl3 t3 inner join
Analysis a
on a.Q_Id = t3.Q_Id
where a.Q_Id = 3;
When you look for the names in your code, look for a_right and t3_right.
Note that you don't need to return Q_Id twice. The ON clause guarantees that the values are the same.

Fetching all Data from Multiple tables based on Single id

I have to select data from multiple tables based on single key value. I have one table called maintable where I will get all the ids from, and i have another 10 tables in the same database which have maintable.id as a foreign key. Now I have to retrieve data from the 10 tables where maintable.id matches in one single table.
The code I have tried is:
$sql = select id from maintable;
$runsql = mysql_query($sql);
while($sqlRow = mysql_fetch_array($runsql ,MYSQL_ASSOC)) {
for($i=1;$i<=10(This is another table count);$i++) {
$servSql = "select * from table.$i where ref_id = ".$sqlRow['id'];
$runServerSql = mysql_query($servSql);
while($serverRow = mysql_fetch_array($runServSql,MYSQL_ASSOC)) {
}
}
}
Try something like this in a join:
SELECT * FROM maintable m
INNER JOIN othertable o
ON m.id = o.id
That will select from both tables using an inner join ON the id column. You may want to look up a basic SQL tutorial to learn the basic types of joins you can use. Good luck!

PHP PDO Multiple Table join

I have multiple tables and I want to access the fields in a specific table. Here are the table schemas:
Each tbl_acct has one tbl_unit. A tbl_unit has many tbl_groups and a tbl_groupcontact consist of the contacts and the related group. Every time I log in, I set a session where $_SESSION['unitid'] = $row['unitid'];.
What I wanted to do is to access the fields in the tbl_contacts. For now my code is here:
$data = $conn->prepare("SELECT * FROM tbl_groups LEFT JOIN tbl_groupcontact ON tbl_groups.id=tbl_groupcontact.group_id WHERE tbl_groups.unitid = ?");
$data->execute(array($_SESSION['unitid']));
foreach ($data as $row) {
echo $row['fname'] . " " . $row['lname']. "<br />";
}
As you can see, I can related the tbl_groups and tbl_groupcontact in my code however, I can't get the fields in the tbl_contacts. Am I missing something here? Any help would be much appreciated.
You need to join another table.
SELECT tbl_contacts.*
FROM tbl_groups
INNER JOIN tbl_groupcontact ON tbl_groupcontact.group_id = tbl_groups.id
INNER JOIN tbl_contacts ON tbl_contacts.id = tbl_groupcontact.contact_id
WHERE tbl_groups.unitid = ?
No need for (slower) LEFT JOIN btw - use it when you want to retrieve records from (left-side) table even when there's no match found in joined (right-side) table (it's columns will be filled with nulls in this case).

Easy SQL statement - Should I JOIN 3 tables here or 2?

I'm a newbie to MySQL and php and I'm getting wrapped around the axle on this one. I've been working on this for hours and can't get the desired result. Help please? What I am starting with is:
$query = sprintf('select image_id, filename from images');
$result = mysql_query($query);
$images = array();
while ($row = mysql_fetch_array($result)) {
$id = $row['image_id'];
$images[$id] = $row['filename'];
echo $images[$id];
}
I have three tables:
submissions - submission_id, price, description
images - image_id, filename, size
imagsub - image_id, submission_id
The above script is pulling out all the filenames and image_id's in the database. I need to limit the results to just the images rows that match up with the submission_id's in the referencing table. Ive tried various JOINS within the sprintf function to no avail and now I cant even get the results I want with a simple mysql_query. Can someone please set me on the right path before I pull my hair out. Do I need to JOIN all three tables here or is it enough to JOIN the images and imagsub tables? I have the submission_id I need in a variable called $item that was pulled from a previous page so maybe I don't need the submissions table at all, just the foreign key from imagsub?
Have you tried
select image_id, filename from images
join imagsub on images.image_id = imagsub.image_id
where imagesub.submission_id = YourID
Try:
select image_id, filename from images i
join imagesub s on s.image_id = i.image_id
where s.subbmision_id = $1;
You will need to set the $1 parameter to the "$item" value you received in the request.
try this as the query to get all of them
select s.submission_id, b.image_id, i.filename
from submissions s
inner join imagsub b on b.submission_id = s.submission_id
inner join images i on i.image_id = b.image_id

Multiple left joins, how to output in php

I have 3 tables I need to join. The contracts table is the main table, the 'jobs' and 'companies' table are extra info that can be associated to the contracts table.
so, since I want all entries from my 'contracts' table, and the 'jobs' and 'companies' data only if it exists, I wrote the query like this....
$sql = "SELECT * FROM contracts
LEFT JOIN jobs ON contracts.job_id = jobs.id
LEFT JOIN companies ON contracts.company_id = companies.id
ORDER BY contracts.end_date";
Now how would I output this in PHP? I tried this but kept getting an undefined error "Notice: Undefined index: contracts.id"...
$sql_result = mysql_query($sql,$connection) or die ("Fail.");
if(mysql_num_rows($sql_result) > 0){
while($row = mysql_fetch_array($sql_result))
{
$contract_id = stripslashes($row['contracts.id']);
$job_number = stripslashes($row['jobs.job_number']);
$company_name = stripslashes($row['companies.name']);
?>
<tr id="<?=$contract_id?>">
<td><?=$job_number?></td>
<td><?=$company_name?></td>
</tr>
<?
}
}else{
echo "No records found";
}
Any help is appreciated.
The column names will not be prefixed like this - and with each table having a column called "id" you could be in trouble. You should explicitly identify the columns you want returned rather than using "select *", and you then just retrieve the column by name un prefixed (e.g. $row['job_number']).
The below would solve you problem.
$sql = "SELECT contracts.id AS contract_id, jobs.job_number, companies.name FROM contracts
LEFT JOIN jobs ON contracts.job_id = jobs.id
LEFT JOIN companies ON contracts.company_id = companies.id
ORDER BY contracts.end_date";
Your problem is likely to be realted to the fact you are using two tables with the field id this is why you should select them as an alias using the mysql as clause.
You may also want to look into using a naming convention for your fields and sticking with it. For example, check out the theory of Hungarian Notation, this would stop issues like this from arrissing.

Categories