I am having problem when querying data from 3 tables (FABRICATION, FABRICATION_QC, AND WEIGHT) and using their values in the PHP. My query is like this:
select fabrication.*,master_drawing.weight,
(select fabrication_qc.marking_qc from fabrication_qc where fabrication_qc.head_mark=fabrication.head_mark) MARKING_QC,
(select fabrication_qc.marking_qc_date from fabrication_qc where fabrication_qc.head_mark=fabrication.head_mark) MARKING_QC_DATE,
(select fabrication_qc.marking_qc_sign from fabrication_qc where fabrication_qc.head_mark=fabrication.head_mark) MARKING_QC_SIGN
from fabrication,fabrication_qc,master_drawing
where fabrication.head_mark = master_drawing.head_mark";
and when I do this in the PHP to get that data into a table,
while (($row = oci_fetch_array($fabParse, OCI_BOTH)) != false)
{
echo '<tr>';
echo '<td>'.$row['PROJECT_NAME'].'</td>';
echo '<td>'.$row['HEAD_MARK'].'</td>';
echo '<td>'.$row['ID'].'</td>';
var_dump($row['MARKING_QC']);
PROJECT_NAME, HEAD_MARK, and ID work fine. Only the dumped MARKING_QC shows NULL in the output.
Can anyone tell me what I'm doing wrong here?
I am assuming this is the query you really want:
select f.*, md.weight,
(select fqc.marking_qc from fabrication_qc fqc where fqc.head_mark = f.head_mark) as MARKING_QC,
(select fqc.marking_qc_date from fabrication_qc fqc where fqc.head_mark = f.head_mark) as MARKING_QC_DATE,
(select fqc.marking_qc_sign from fabrication_qc fqc where fqc.head_mark = f.head_mark) as MARKING_QC_SIGN
from fabrication f join
master_drawing md
on f.head_mark = md.head_mark;
This removes the reference fabrication_qc in the outer from clause. That simply causes an unnecessary cartesian product. I also introduced table aliases to make the query more readable. And, put in proper explicit join syntax rather than the implicit joins.
You can further simplify this to:
select f.*, md.weight,
fqc.marking_qc, fqc.marking_qc_date, fqc.marking_qc_sign
from fabrication f join
master_drawing md
on f.head_mark = md.head_mark left outer join
fabrication_qc fqc
on fqc.head_mark = f.head_mark
I would write the query like this:
select
f.project_name,
f.head_mark,
f.id,
m.weight,
qc.marking_qc,
qc.marking_qc_date,
qc.marking_qc_sign
from fabrication as f
join master_drawing as m
on f.head_mark = m.head_mark
left outer join fabrication_qc as qc
on f.head_mark = qc.head_mark
-- where (no criteria given)
;
Try running the query from a query execution tool outside of your code, to confirm the results.
Related
I have this query in my php and it seems to be fetching the wrong data set from my db.
$querystring = "
SELECT a.*,
b.itemcolour,
b.itemcolourname
FROM itemorders AS a
INNER JOIN catalogueitemscolour AS b
ON a.colourid = b.colourid
WHERE a.colourid IN(SELECT colourid
FROM itemorders
WHERE orderid = 61)
";
Here is a picture of my results
Can I know why it's not selecting the specific orderID of 61?
You can try below -
SELECT a.*, b.itemColour,b.itemColourName FROM itemorders
AS a INNER JOIN CatalogueItemsColour AS b ON a.colourID = b.colourID WHERE
a.orderID = 61
Because you are not putting condition on orderID, rather putting it on colorID. What you actually want is this condition: WHERE a.orderID = 61.
I have created an INNER JOIN query as shown below and was wondering how I can make it work? I need for the HomeTeam and AwayTeam to equal TeamID in the query. Any help would be much appreciated. Thanks
$result = mysqli_query($con,"SELECT results.*,team.TeamName
FROM results
INNER JOIN team ON team.TeamID = results.HomeTeam
INNER JOIN team on team.TeamID = results.AwayTeam");
You need to use aliases for the table that you are including twice. Otherwise mysql cannot distinguish between the two.
To be able to process the results easily, you can do the same with the names you are selecting.
Something like:
SELECT
results.*,
t1.TeamName AS TeamNameHome,
t2.TeamName AS TeamNameAway
FROM results
INNER JOIN team t1
ON t1.TeamID = results.HomeTeam
INNER JOIN team t2
ON t2.TeamID = results.AwayTeam
I am applying Left Join with Sub query & where clause
It seems fine no syntax error but the columns I am selecting from that sub query always returns me Null. I have executed the same part in my SQL section, it gives me record. Kindly have a look on the query and let me know if any thing is possible or if question is not clear.
SELECT alt.userId, u.name, t.name AS teamName, alt.startDateTime, v.name AS villageName, c.name AS clusterName, startLat, startLong, latlng.lat, latlng.long
FROM activity_log_tim AS alt
JOIN user AS u ON u.userId = alt.userId
JOIN team_members AS tm ON tm.memberId = u.userId
JOIN team AS t ON t.teamId = tm.teamId
JOIN village AS v ON v.villageId = alt.villageId
JOIN cluster_villages AS cv ON cv.villageId = v.villageId
JOIN cluster AS c ON c.clusterId = cv.clusterId
LEFT JOIN (SELECT lat, long,dateTime, scheduleId FROM activity_log_gps LIMIT 1) AS latlng ON latlng.scheduleId = alt.scheduleId
WHERE DATE(alt.startDateTime) = '2015-09-05' AND DATE(alt.endDateTime) = '0000-00-00' GROUP BY alt.userId ORDER BY latlng.dateTime DESC
Well it's supposed to since you are performing a LEFT JOIN which produces NULL if no match occurs. So either you can perform a INNER JOIN or use a COALESCE function like below
SELECT alt.userId, u.name,
t.name AS teamName,
alt.startDateTime,
v.name AS
villageName,
c.name AS clusterName,
startLat,
startLong,
COALESCE(latlng.lat, 23), //Notice the use of COALESCE. If null will return 23 as default
COALESCE(latlng.long, 32)
I have this statement:
SELECT personen.*, klassen.naam as klas FROM `personen`
JOIN `klassen` ON `klassen`.id = `personen`.Id
WHERE `rol` = "Docent" ORDER BY id
I know that it basically puts the column klassen.naam into personen by checking if the foreign key K_Id is the same as the id of Klassen:
In the table above I have 4 results, these are all the people who have a K_Id assigned, however there are 2 more people who have a K_Id of null:
My problem is that when K_Id is null it doesn't return anything at all, which makes sense, I just don't know how to fix it.
My question is: How can i write the statement so that it still shows all the other rows where the K_Id value is null?
SELECT personen.*, klassen.naam as klas
FROM `personen`
JOIN `klassen`
ON `klassen`.id = `personen`.Id
WHERE `rol` = "Docent"
ORDER BY id
You should use left join if you want all of your data from personen Table where personen.id is not present in klassen table it look like this
SELECT personen.*, klassen.naam as klas
FROM `personen`
LEFT JOIN `klassen`
ON `klassen`.id = `personen`.Id
WHERE `rol` = "Docent"
ORDER BY id
You would use a left join for this purpose. Assuming rol is in the personen table:
SELECT p.*, k.naam as klas
FROM `personen` p LEFT JOIN
`klassen` i
ON i.id = p.Id
WHERE p.`rol` = 'Docent'
ORDER BY p.id;
If rol is in the klassen table, then:
SELECT p.*, k.naam as klas
FROM `personen` p LEFT JOIN
`klassen` i
ON i.id = p.Id AND i.`rol` = 'Docent'
ORDER BY p.id;
Note that I also introduced table aliases in the query. These make queries easier to write and to read.
Also, this will return all rows in the personen table (subject to the where), including non-matches. If you just specifically wanted NULL values, then you would use:
SELECT p.*, k.naam as klas
FROM `personen` p JOIN
`klassen` i
ON i.id = p.Id or p.id is null
WHERE p.`rol` = 'Docent'
ORDER BY p.id;
I'm guessing you really want the left join version.
Is there a way to avoid adding a second LEFT JOIN for the table "social_mcouple" to query where social_members.m_id = social_mcouple.c_id below?
$res = #mysql_query("SELECT *, DATE_FORMAT(m_lld,'%m/%d/%y') AS m_lld_formatted FROM social_members
LEFT JOIN social_member_types ON t_id=m_type WHERE m_user='".$en['user']."'");
If there will always be a social_mcouple that corresponds to social_members, or you're only interested in rows where there is a correspondence then you may use an INNER JOIN. If you need all social_members regardless of whether there is a corresponding social_mcouple then you will need a LEFT JOIN. The LEFT JOIN will give you all rows with social_mcouple.* set to NULL where there is not a match.
The performance hit will really depend on the size of your datasets.
EDIT: adding a sample UNION query.
$res = #mysql_query("
(SELECT social_members.*, social_member_types.*, DATE_FORMAT(m_lld,'%m/%d/%y') AS m_lld_formatted,
NULL AS mcouple1, NULL AS mcouple2, NULL AS mcouple3
FROM social_members
LEFT JOIN social_member_types ON t_id=m_type
WHERE m_user='".$en['user']."' AND m_type != 2)
UNION
(SELECT social_members.*, social_member_types.*, DATE_FORMAT(m_lld,'%m/%d/%y') AS m_lld_formatted,
social_mcouple.mcouple1, social_mcouple.mcouple2, social_mcouple.mcouple3
FROM social_members
LEFT JOIN social_member_types ON t_id=m_type
JOIN social_mcouple ON social_members.m_id = social_mcouple.c_id
WHERE m_user='".$en['user']."' AND m_type = 2)
");