get in single MySql query from this four - php

want to combine this 4 query in single mysql query and my current query is
$sql1 = "SELECT * FROM projectFiles pf
INNER JOIN projectFileFolders pff ON pf.folderID = pff.folderID
WHERE pff.projectID = '".$row['projectID']."' AND folderType=3"
$sql2 = "SELECT * FROM projectFiles pf
INNER JOIN projectProducts pp ON pf.fileID = pp.fileID
WHERE pp.projectID = '".$row['projectID']."'"
$sql3 = "SELECT * FROM projectFiles pf
INNER JOIN projectMaintenance pm ON pf.fileID = pm.fileID
WHERE pm.projectID = '".$row['projectID']."' "
$sql4 = "SELECT * FROM projectFiles pf
INNER JOIN projectServices ps ON pf.fileID = ps.fileID
WHERE ps.projectID = '".$row['projectID']."' "

If I understand your requirement correctly, you have four tables, and you want to include any records from those tables whose projectID matches the one in your provided $row['projectID'] field. If so, you can simply combine all your queries like this:
$sqlWhatever =
"SELECT * FROM projectFiles pf
INNER JOIN projectFileFolders pff ON pf.folderID = pff.folderID
INNER JOIN projectProducts pp ON pf.fileID = pp.fileID
INNER JOIN projectMaintenance pm ON pf.fileID = pm.fileID
INNER JOIN projectServices ps ON pf.fileID = ps.fileID
WHERE pff.projectID = '".$row['projectID']."' AND folderType=3
OR pp.projectID = '".$row['projectID']."'
OR pm.projectID = '".$row['projectID']."'
OR ps.projectID = '".$row['projectID']."' "
Now, you haven't said whether each of the four tables has the same fileID's or not. If they do not, then change your INNER join to a LEFT join, or you'll only get records that have a common fileID in each of the four tables.

Related

Combine Two Queries into One?

I have a MySQL query that selects all audioids from a certain user in a subscribe table.
I then have another query which takes this list of audioids and matches them against a field called opids in a table called audioposts. It then selects the titles from that audioposts table and joins the users from a users table at the userid.
Is there a way I can turn these two queries into one query?
query1 = "SELECT audioid FROM subscribe WHERE userid = $userid";
query2 = "SELECT ap.audioid, ap.title, us.name FROM audioposts ap
INNER JOIN audioposts a2 ON a2.audioid = ap.opid
INNER JOIN users us ON us.id = a2.userid
WHERE ap.opid = $newaudio";
Here is the current two query code which I'd like to replace with one query. I've not yet translated this into prepared statements as it's easier for me to visualize it the old-fashioned way. Plus I'll be converting this into NodejS eventually anyway;
$selectaudioid = "SELECT audioid FROM subscribe WHERE userid = $userid";
$audioResult=$dblink->query($selectaudioid);
if ($audioResult->num_rows>0) {
while ($row = $audioResult->fetch_assoc()) {
$newaudio = $row[audioid];
$getallaudio = "
SELECT ap.audioid, ap.title, us.name FROM audioposts ap
INNER JOIN audioposts a2 ON a2.audioid = ap.opid
INNER JOIN users us ON us.id = a2.userid
WHERE ap.opid = $newaudio AND ap.opid <> '0'";
$getallresult = $dblink->query($getallaudio);
if ($getallresult->num_rows>0) {
while ($row = $getallresult->fetch_assoc()) {
$dbdata[]=$row;
}}}}
Just add another join with subscribe.
SELECT ap.audioid, ap.title, us.name
FROM audioposts ap
INNER JOIN audioposts a2 ON a2.audioid = ap.opid
INNER JOIN subscribe s ON s.audioid = ap.opid
INNER JOIN users us ON us.id = a2.userid
WHERE s.userid = $userid AND ap.opid <> '0'

How to correct Conditional WHERE and JOIN sql table request?

I'm trying to select an specific information from a table that depends on the conditions of two other tables. When I try separate conditions work, but when I try to work with two conditions does not.
With this I get the correct data from table:
$sql = "SELECT * FROM commerce_order WHERE m_key = 'total'";
And with this I also get the result from table:
$sql = "SELECT *
FROM commerce_order M
JOIN commerce_order_items I
ON M.item_id = I.item_id
JOIN produtcs P
ON I.order_id = P.ID
WHERE status = 'complete'";
but when I try this I don't get any result:
$sql = "SELECT *
FROM commerce_order M
WHERE m_key = 'total'
JOIN commerce_order_items I
ON M.item_id = I.item_id
JOIN produtcs P
ON I.order_id = P.ID
WHERE status = 'complete'";`
I expected the first code result to be filtered by the second code condition. I know which are the values from the first table that should return, but I don't get any.
You can't put WHERE m_key = 'total' before JOIN all your WHERE clauses should go after all your JOINs:
$sql = "SELECT *
FROM commerce_order M
JOIN commerce_order_items I
ON M.item_id = I.item_id
JOIN produtcs P
ON I.order_id = P.ID
WHERE status = 'complete' AND m_key = 'total'";

insert php code in sql query

function search_num_rows($param){
$company_name=$param['company_name'];
$loan_no=$param['loan_no'];
$q = $this->db->query("select Count(0) as num_rows
from contact_new
inner join companies c on contact_new.company_id = c.id
inner join history on contact_new.id = history.receiver_email
inner join escalation_level on contact_new.escalation_level_id = escalation_level.id
inner join departments on contact_new.departmend_id = departments.id
WHERE loan_no= '$loan_no' if($company_name){ AND company_name= '$company_name'} ")->result();
return $q[0]->num_rows;
}
can i insert the php code as i done in where clause.Is there any other way to do this without using active records.
It's actually very easy:
function search_num_rows($param){
$company_name = (isset($param['company_name']) && !empty($param['company_name']) ? " AND company_name = '$param[company_name]'" : '');
$loan_no=$param['loan_no'];
$q = $this->db->query("select Count(0) as num_rows
from contact_new
inner join companies c on contact_new.company_id = c.id
inner join history on contact_new.id = history.receiver_email
inner join escalation_level on contact_new.escalation_level_id = escalation_level.id
inner join departments on contact_new.departmend_id = departments.id
WHERE loan_no= '$loan_no' $company_name")->result();
return $q[0]->num_rows;
}

Extracting single data using limit with 5 tables involve

so i have 5 tables in which it is interconnected with foreign keys
and here is a sample output of table 3
what i wanted to do in table number 3 is to extract the SubdeptID of user with userid of 10 but in this case it has 2 userid10 so its print both. what i want to print is only the one with latter TransferID. my select statement is this
$sql_exp = "SELECT a.UserID, b.Employeename, c.TransferID, e.Department
FROM dbo.FA_Laptop a
INNER JOIN dbo.users b
on a.UserID = b.UserID
INNER JOIN dbo.SubDeptTransfer c
ON a.UserID = c.UserID
INNER JOIN dbo.SubDept d
ON c.SudDeptID = d.SubDeptID
INNER JOIN dbo.departments e
ON d.DeptID = e.DeptID
WHERE a.FAID = '$faidf' ORDER by c.TransferID DESC LIMIT 1";
my php code is
$rs = $conn->Execute($sql_exp);
if ($rs->EOF) {
echo "<tr><td>Please check for the Employee Name or the Department</td>";
} else {
while (!$rs->EOF){
echo "<tr><td>".$rs->Fields("Department")." / ".$rs->Fields("EmployeeName")."</td>";
$rs->movenext();
}
$rs->Close();
}
im having an error in "LIMIT" query.
MSSQL don't have LIMIT keyword.
Use TOP instead LIMIT.
$sql_exp = "SELECT TOP 1 a.UserID, b.Employeename, c.TransferID, e.Department
FROM dbo.FA_Laptop a
INNER JOIN dbo.users b
on a.UserID = b.UserID
INNER JOIN dbo.SubDeptTransfer c
ON a.UserID = c.UserID
INNER JOIN dbo.SubDept d
ON c.SudDeptID = d.SubDeptID
INNER JOIN dbo.departments e
ON d.DeptID = e.DeptID
WHERE a.FAID = '$faidf' ORDER by c.TransferID DESC";

Displaying results from multiple tables SQL/PHP

I currently have a database with 12 tables. I am doing a php query to pull the information from the database but I am not getting anything displayed. The query alone bridges all the tables starting with table schedule that have a foreign key related. Should I need to start the query from the table class and bridge with other tables?
TABLE DESIGN- PICTURE
If you like to duplicate my design- QUERY
$query = ("SELECT class_name, class_caption, class_credit_hours, class_description
FROM schedule
INNER JOIN section
ON class.id = section.class_id
INNER JOIN faculty
ON faculty.id = section.faculty_id
INNER JOIN faculty
ON faculty.id = office_hours.faculty_id
INNER JOIN faculty_titles
ON faculty_titles.faculty_id = faculty.id
INNER JOIN faculty_education
ON faculty_education.faculty_id = faculty.id
INNER JOIN section
ON section.faculty_id = faculty.id
INNER JOIN class
ON class.id = section.class_id
INNER JOIN major_class_br
ON major_class_br.class_id = class.id
INNER JOIN major_minor
ON major_class_br.major_minor_id = major_minor.id
");
//execute query
$result = mysql_query($query);
if ($result){
$totalhours = 0;
while ($row = mysql_fetch_assoc( $result ))
{
print "<b>" . $row['class_name'] . "</b><br>";
print $row['class_caption'] . "<br>";
print $row['class_description'] . "<br>";
print $row ['class_credit_hours'] . "hrs. <br>";
print "------------------------------<br />";
$totalhours += $row['class_credit_hours'];
}
}
SQL fiddle query
SELECT class_name, class_caption, class_credit_hours, class_description
FROM schedule
INNER JOIN section
ON class.id = section.class_id
Right here there's a problem: you are doing a INNER JOIN using the field 'class.id' but the table 'class' isn't in either side of the JOIN. So it won't work.
The query should start like this:
SELECT class_name, class_caption, class_credit_hours, class_description
FROM class
INNER JOIN section
ON class.id = section.class_id
And then do the JOIN with the table 'schedule' with the table it shares a common index (I guess it would be class).
The complete query should be something like this:
SELECT class.class_name, class.class_caption, class.class_credit_hours, class.class_description
FROM class
INNER JOIN section
ON class.id = section.class_id
INNER JOIN faculty
ON faculty.id = section.faculty_id OR faculty.id = office_hours.faculty_id
INNER JOIN faculty_titles
ON faculty_titles.faculty_id = faculty.id
INNER JOIN faculty_education
ON faculty_education.faculty_id = faculty.id
INNER JOIN major_class_br
ON major_class_br.class_id = class.id
INNER JOIN major_minor
ON major_class_br.major_minor_id = major_minor.id
INNER JOIN sched_sect_br
ON sched_sect_br.section_id = section.id
INNER JOIN schedule
ON schedule.id = sched_sect_br.schedule_id
INNER JOIN semester
ON semester.id = schedule.semester_id
INNER JOIN office_hours
ON schedule.id = office_hours.schedule_id AND faculty.id = office_hours.faculty_id
This query gets info from all the tables you have in your graph, aside from the event table, that isn't related to any other. But still this query should have more fields on the SELECT (you are only selection fields from the class table, I understand you'd want data from the other 10 tables as well), to do this simple add 'tablename.field' to the list of fields from the SELECT.

Categories