Sanity check: Will this SQL query join how I want? - php

My deepest apologies for the query, that's legacy code for you. I basically want to check my code makes logical sense.
Essentially I want to return all rows of the "signatures" table where the "claim_id" = the "claim_id" on the "claims" table.
From that, I intend to check if the "signature_id" = null for each claim. If it does, that claim has no related signature, correct?
$SQL="
SELECT
`claims`.`claim_id`,
CONCAT(`insurers`.`insurer_name`,IF((`insurers`.`insurer_branch`<>''),CONCAT(' - ',`insurers`.`insurer_branch`),'')) as 'insurer',
`claims`.`claim_number` as 'claim_number',
UNIX_TIMESTAMP(claims.overflow_date) as 'overflow_date',
UNIX_TIMESTAMP(claims.reassigned_date) as 'reassigned_date',
UNIX_TIMESTAMP(claims.reassigned_from_fr_date) as 'reassigned_from_fr_date',
UNIX_TIMESTAMP(claims.date_of_first_contact) as 'date_of_first_contact',
UNIX_TIMESTAMP(`claims`.`date_surveyor_made_contact`) as 'date_surveyor_made_contact',
UNIX_TIMESTAMP(`claims`.`date_of_inspection`) as 'date_of_inspection',
UNIX_TIMESTAMP(`claims`.`surveyor_completed`) as 'surveyor_completed',
UNIX_TIMESTAMP( CASE 1 WHEN (claims.overflow_date>0) THEN claims.overflow_date WHEN (claims.reassigned_date>0) THEN claims.reassigned_date WHEN (claims.reassigned_from_fr_date>0) THEN claims.reassigned_from_fr_date ELSE claims.date_of_first_contact END ) as 'date_of_first_contact_debug',
`claims`.`date_surveyor_made_contact` as 'date_surveyor_made_contact_debug',
`claims`.`date_of_inspection` as 'date_of_inspection_debug',
`claims`.`surveyor_completed` as 'surveyor_completed_debug',
`claims`.`letter_of_findings`,
`claims`.`int_settlement_reason`,
`claims`.`ext_settlement_reason`,
`claims`.`original_surveyor_id`,
`claims`.`surveyor_id`,
`claims`.`broker_name`,
`insurers`.`insurer_type`,
`insurers`.`insurer_id`,
`signatures`.`signature_id`,
`signatures`.`claim_id`
FROM
(((`claims` claims INNER JOIN `insurers` insurers ON claims.`insurer_id` = insurers.`insurer_id`)
INNER JOIN `users` users ON claims.surveyor_id = users.user_id)
LEFT JOIN `signatures` signatures on claims.claim_id = signatures.claim_id)
WHERE
(`claims`.`claim_type` <> ".$conn->qstr(TYPE_DESKTOP).") AND
(`claims`.`claim_type` <> ".$conn->qstr(TYPE_AUDIT).") AND
(`claims`.`insurer_id` NOT IN ('".implode("','",$arrSURGE)."')) AND
(`users`.`status` = ".$conn->qstr(STATUS_LIVE).") AND
(`claims`.`claim_cancelled_id` = 0) AND
$where
";
The code I've added is both the signatures. selectors, and the LEFT JOIN 'signatures' signatures on claims.claim_id = signatures.claim_id) line.
claim_id is PK on the claims table, and signature_id is PK on signatures table.
Thanks!

The null check will only shows items with signatures. If you leave the null check out of the picture, you will get nulls for items where signature is not present.
check this

Related

Get the records using left join

I try to get the records from opportunity that created manually, so it means it’s not created via Converted from Leads or other module.
I’m trying to do is to get the opportunity record that is not converted from leads.
Below, you will see my query using left join the leads to opportunity using
opportunity id from leads table and the opportunity id from Opportunity table.
But whenever I try to run this query it doesn’t show the records that created manually in the opportunity,
I just want to get the records that are not converted and created manually in the opportunities. May I know why it is not showing? Thanks guys.
$strQuery = " SELECT
*
FROM
leads l
LEFT JOIN
opportunities O
ON
l.opportunity_id = O.id
WHERE
l.deleted = '0'
AND
O.deleted = '0'
AND
l.converted = '0'
AND
DATE_FORMAT(O.date_created, '%y-%m-%d') = CURDATE()";
//
$hQuery = $db->query($strQuery);
//
while ( $arRow = $db->fetchByAssoc($hQuery) ){
// My logic
}
I just want to get the records that are not converted and created manually in the opportunities
You may need to reverse the table relationships, e.g.
SELECT *
FROM opportunities O
LEFT JOIN leads l ON O.id = l.opportunity_id
WHERE l.opportunity_id IS NULL
AND O.deleted = '0'
AND DATE_FORMAT(O.date_created, '%y-%m-%d') = CURDATE()
here there is no assocated "lead", just an "opportunity".

Return all rows from table one even if table two is empty or partial results found

I am trying to return all the results from table one, AKA ship_skill_tree, while matching up the rows found in table two, AKA character_sheet_skills, even if the rows do not exist in table two.
SELECT c.`level` , t.`skillLevel` AS levelNeeded, i.`typeName`
FROM `ship_skill_tree` t
LEFT JOIN `character_sheet_skills` c ON t.`skillTypeID` = c.`typeID`
LEFT JOIN `invTypes` i ON i.`typeID` = t.`skillTypeID`
WHERE t.`shipTypeID` = 11176 AND c.`character_id` = 1;
Table One Data:
|shipTypeID|shipGroupID|skillTypeID|skillLevel
______________________________________________
|11011|26|3332|1
|11129|31|3327|1
|11132|31|3327|1
|11134|31|3327|1
|11172|830|3328|5
|11172|830|12093|1
|11174|893|3328|5
|11174|893|28615|1
|11176|831|3330|5
|11176|831|12092|1
Table Two Data:
|character_id|typeID|skillpoints|level|published
______________________________________________
|1|3300|1415|2|1
|1|3301|8000|3|1
|1|3327|256000|5|1
|1|3330|2829|2|1
|1|3340|181020|4|1
|1|3341|1024000|5|1
|1|3342|32000|3|1
|1|3343|32202|3|1
|1|3380|256000|5|1
|1|3385|256000|5|1
|1|3386|256000|5|1
|1|3392|256000|5|1
|1|3394|90514|4|1
|1|3402|256000|5|1
|1|3410|768000|5|1
|1|3411|135765|4|1
|1|3412|750|1|1
|1|3413|256000|5|1
|1|3416|45255|4|1
|1|3417|0|0|1
|1|3418|0|0|1
|1|3419|135765|4|1
|1|3420|181020|4|1
|1|3423|0|0|1
|1|3425|90510|4|1
|1|3426|45255|4|1
|1|3428|500|1|1
|1|3429|8000|3|1
|1|3436|45255|4|1
|1|3437|45255|4|1
|1|3438|500|1|1
|1|3449|256000|5|1
|1|3453|0|0|1
|1|3455|256000|5|1
|1|3456|226275|4|1
|1|11579|271530|4|1
|1|12186|0|0|1
|1|12187|0|0|1
|1|12188|0|0|1
|1|12190|22547|3|1
|1|12191|45255|4|1
|1|12192|45255|4|1
|1|12193|45255|4|1
|1|12195|45255|4|1
|1|16281|256000|5|1
|1|17940|1024000|5|1
|1|20342|1280000|5|1
|1|22551|40000|3|1
|1|22578|181020|4|1
|1|25739|0|0|1
|1|26252|16000|3|1
|1|26253|750|1|1
|1|26261|750|1|1
|1|32918|16000|3|1
invTypes table:
|typeID|typeName
________________
|3327|Spaceship Command
|3328|Gallente Frigate
|3330|Caldari Frigate
|3332|Gallente Cruiser
|12092|Interceptors
|12093|Covert Ops
|28615|Electronic Attack Ships
In the above query shipTypeID will always, or should always, be valid and match a record in table one, however, in table two, the rows that match may not exist. What I need is to output as follows:
|level|levelNeeded|typeName
___________________________
|2|5|Caldari Frigate
|NULL|1|Interceptors
Currently this is what is returned:
|level|levelNeeded|typeName
___________________________
|2|5|Caldari Frigate
EDIT: Solution!
SELECT c.`level` , t.`skillLevel` AS levelNeeded, i.`typeName`
FROM `ship_skill_tree` t
LEFT JOIN `character_sheet_skills` c ON t.`skillTypeID` = c.`typeID` AND c.`character_id` = 1
INNER JOIN `invTypes` i ON i.`typeID` = t.`skillTypeID`
WHERE t.`shipTypeID` = 11176
You need to put any restrictions on the table being joined in the ON clause. If you put them in the WHERE clause it doesn't work, because the rows that don't have any matches will produce NULL for those columns, and the WHERE clause will filter them out.
SELECT c.`level` , t.`skillLevel` AS levelNeeded, i.`typeName`
FROM `ship_skill_tree` t
LEFT JOIN `character_sheet_skills` c ON t.`skillTypeID` = c.`typeID` AND c.`character_id` = 1
LEFT JOIN `invTypes` i ON i.`typeID` = t.`skillTypeID`
WHERE t.`shipTypeID` = 11176
DEMO
You need to use a right join or an outer join rather than a left join. Have a look through the Visual Representation of SQL Joins for a good overview

JOIN Query while loading comments

I'm loading comments for product with id = '3'
$get_comments = mysql_query("SELECT * FROM products_comments WHERE product_id = '3'");
Now I want to add the "report abuse" option for each comment, for this purpose I'm having another table as "abuse_reports" which user abuse reports will be stored in this table, now if a user reported a comment, the report abuse option should not be there for that comment for that user there anymore, for this I'm doing:
while($row = mysql_fetch_array($get_comments)){
echo blah blah blah // comment details
// now for checking if this user should be able to report this or not, i make this query again:
$check_report_status = mysql_query("SELECT COUNT(id) FROM abuse_reports WHERE reporter_user_id = '$this_user_id' AND product_id = 'this_product_id'");
// blah blah count the abuse reports which the current user made for this product
if($count == 0) echo "<a>report abuse</a>";
}
With the above code, for each comment I'm making a new query, and that's obviously wrong, how I should join the second query with the first one?
Thanks
Updated query (that is working now, commited by questioner)
SELECT pc. * , count( ar.`id` ) AS `abuse_count`
FROM `products_comments` pc
LEFT OUTER JOIN `abuse_reports` ar ON pc.`id` = ar.`section_details`
AND ar.`reporter_id` = '$user_id'
WHERE pc.`product_id` = '$product_id'
GROUP BY pc.`id`
LIMIT 0 , 30
The query works as follow: You select all the fields of your products_comments with the given product_id but you also count the entries of abuse_reports for the given product_id. Now you LEFT JOIN the abuse_reports, which means that you access that table and hang it on to the left (your products_comments table). The OUTER allows that there is no need for a value in the abuse_reports table, so if there is no report you get null, and therefore a count of 0.
Please read this:
However, I needed to group the results, otherwise you get only one merged row as result. So please extend your products_comments with a field comment_id of type int that is the primary key and has auto_increment.
UPDATE: abuse count
Now you can do two things: By looping through the results, you can see for each single element if it has been reported by that user or not (that way you can hide abuse report links for example). If you want the overall number of reports, you just increase a counter variable which you declare outside the loop. Like this:
$abuse_counter = 0;
while($row = mysql....)
{
$abuse_counter += intval($row['abuse_count']); // this is 1 or 0
// do whatever else with that result row
}
echo 'The amount of reports: '.$abuse_counter;
Just a primitive sample
I believe your looking for a query something like this.
SELECT pc.*, COUNT(ar.*)
FROM products_comments AS pc
LEFT JOIN abuse_reports AS ar ON reporter_user_id = pc.user_id AND ar.product_id = pc.product_id
WHERE product_id = '3'"
try this SQL
SELECT pc.*, COUNT(ar.id) AS abuse_count
FROM products_comments pc
LEFT JOIN abuse_reports ar ON pc.product_id = ar.product_id
WHERE pc.product_id = '3' AND ar.reporter_user_id = '$this_user_id'
GROUP BY pc.product_id
The result is list of products_comments with abuse_reports count if exist for reporter_user_id

Error handling in the following sql query

Fiddle with tables here
I'm using the following sql with the tables in the fiddle to check if a user has reached the borrowing limit. The problem here is, If an invalid item number were supplied it returns NULL, if a user has not borrowed any items, it returns NULL. This way, I cannot tell if a invalid item number were supplied or if a user actually has not borrowed any books. What would be a good way to check if a invalid item number was supplied or a member actually has not borrowed anything under that category?
set #mId = 3 //Has not borrowed anything till now.
set #id = 21; //This item does not appear in the collection_db table and is therefore invalid.
set #country = 'US';
SELECT col1.id, col1.holder, col2.borrowMax maxLimit, count(lend.borrowedId) as `count`
FROM collection_db col1
INNER JOIN collection_db col2
ON col1.holder = col2.id
INNER JOIN lendings lend
ON col1.holder = lend.holder and col1.country = lend.country
WHERE col1.id = #id and col1.country = #country
AND col2.category = 10
AND lend.memId = #mId and lend.country = #country
The furthest I could get with the one query is (had to take out php and "country" vars for fiddle to work):
SELECT col1.id, col1.holder, col2.borrowMax maxLimit, count(lend.borrowedId) as `count`
,case when valid1.id is not null then 'true' else 'false' end as validId
FROM collection_db col1
INNER JOIN collection_db col2
ON col1.holder = col2.id
INNER JOIN lendings lend
ON col1.holder = lend.holder,(
Select Distinct a.id From collection_db a
Where a.id = 4) valid1
WHERE col1.id = 4
AND col2.category = 10
AND lend.memId = 1
You may have to do a preparatory query checking for a valid memId:
$theQuery = "SELECT DISTINCT memId FROM lendings WHERE memId = 1"
Then test it here:
if (mysql_num_rows(mysql_query($theQuery)) <= 0) { /* No memId exists */ }
else { /* Do big query here */ }
You can use a tableA LEFT JOIN tableB, which will return results for the tableA even if tableB has no matches and will return NULL values for those in tableB.
Unfortunately, I can't quite figure out where you need LEFT JOINS, but probably you want them in both places.
You also might have to reorder the tables if it is the first table that should be on the right side of a LEFT JOIN. You could use a RIGHT JOIN but it is less readable to me.
maybe you should try "left join" if col1 do not have too much data,or do the query step by step

Combining Multiple MySQL JOIN Queries

I have this query which echos IDs of assignments for classes which users are enrolled in.
$sql = $db->prepare("SELECT assignments.*, enrollments.course_id, enrollments.student_id
FROM assignments
LEFT JOIN enrollments
ON assignments.course_id = enrollments.course_id
LEFT JOIN completed
ON assignments.id != completed.assignment_id
WHERE enrollments.student_id = ?
ORDER BY assignments.id DESC LIMIT 10
");
$sql->execute(array($login_id));
while($row = $sql->fetch())
{
echo $row['id'];
}
What would be the best way to do yet another check where I see if the assignment has been marked as completed?
This means that it would also need to check the "completed" table and make sure there is no row where the $login_id and assignment.id are present together for any of the assignments selected.
Here's a query I have right now to find completed assignment IDs for a user logged in.
$sqlcomplete = $db->prepare("SELECT * FROM completed
INNER JOIN students ON completed.student_id = students.id
WHERE completed.student_id = ?
");
$sqlcomplete->execute(array($login_id));
while($row = $sqlcomplete->fetch(PDO::FETCH_ASSOC))
{
echo "<li>You have completed assignment with ID ".$row['assignment_id']."</li>";
}
I've tried to do a more complex JOIN but I can't seem to figure it out. I also considered simply creating an array of the IDs of the assignments which the user has completed by querying that database alone, and throwing that ID into the while check, but I feel like that is not the best or most efficient solution.
You can use a LEFT JOIN and when completed.assignment_id IS NULL then that means there was no match returned from the completed table.
SELECT assignments.*, enrollments.course_id, enrollments.student_id
FROM assignments
LEFT JOIN enrollments ON assignments.course_id = enrollments.course_id
LEFT JOIN completed ON assignments.id = completed.assignment_id
WHERE enrollments.student_id = ?
AND completed.assignment_id IS NULL
ORDER BY assignments.id DESC LIMIT 10

Categories