Trying to do a LEFT JOIN, but ending up getting empty result - mysql

I tried doing a LEFT JOIN (the one on the image second of the left). So basically I want all records from table A without the records from table B, so I want to take only the information from table B that crosses with table A. There is more info like book details in table B that I want to be able to extract, but I don't need of the rows from table B, just the extra info. I figure the best way to do this is with LEFT JOIN and this is the code I tried, but it result in a empty string/result:
SELECT WF.*, W.* FROM WalletFrom AS WF LEFT JOIN Wallet AS W ON W.BookID = WF.BookID WHERE WF.BookID = 1360 AND W.BookID IS NULL
What I am doing wrong?
Columns from Wallet:
WalletID
UserID
BookID
OrderID
WalletStatusID
Win
WalletProductID
Columns from WalletFrom:
OrderID
BookID
Count
Win
WinTotal
Tax
WinEnd
DateTime
WalletProductID

You get empty result, because of W.BookID IS NULL clause, which is guaranteed to be not null by left join and its condition.
If you want only recrods from WalletFrom with BookId existing in Wallet, you could use below query:
SELECT * FROM WalletFrom wf
WHERE EXISTS(SELECT 1 FROM Wallet
WHERE BookId = wf.BookId)

What I end up needing to do is just add an AND cause to my JOIN cause like this
SELECT WF.*, W.* FROM WalletFrom AS WF LEFT JOIN Wallet AS W ON W.BookID = WF.BookID AND W.OrderID = WF.OrderID WHERE WF.BookID = 1360
In order to not get duplicated records/rows. Thanks for everyone who have answer my questions you helped me to figure this out

According to what you say in the comments, this sounds like inner join.
SELECT *
FROM TableA
INNER JOIN TableB ON TableA.BookID = TableB.BookID
WHERE TableA.BookiD = 1360
this gives you all the records from tableA that have a corresponding row in TableB without extra rows from TableB.
Ceck if this is what you need
Update
SELECT *
FROM Students AS S
INNER JOIN Grades AS G ON s.StudentId = G.StudentId
WHERE S.ClassId = 1360

Related

Return all data from 1st Table, and only 1 data from 2nd Table if exist

I am using MySql.
I have 2 tables, one is a list of names and data with primary key propertyId.
table 2 contains images with primary key propertyImageId.
Each propertyId may have multiple images or NO images at all.
I need to get a list of all the propertyId that belongs to agentId = 1, regardless whether it has images or not.
'SELECT a.*, b.*
FROM property a LEFT OUTER JOIN property_images b
ON a.propertyId = b.propertyId
INNER JOIN
( SELECT propertyId, MAX(created) maxCreated
FROM property_images
GROUP BY propertyId) c
ON b.propertyId = c.propertyId ANd b.created = c.maxCreated
WHERE agentId = 1 ');
I'm trying a similar solution provided here MySQL INNER JOIN select only one row from second table
However, it only returns propertyId if images exist. What can I do so that it will return all the propertyId from property regardless whether property_images exist or not?
Been working on this, any help will be deeply appreciated. Thank you!!
You can rewrite your query as below, The inner query gets single image per property id with highest created column value
SELECT
a.*,
b.*
FROM
property a
LEFT JOIN
(SELECT
c.*
FROM
property_images c
LEFT JOIN property_images d
ON c.propertyId = d.propertyId
AND c.created < d.created
WHERE d.propertyId IS NULL) b
ON a.propertyId = b.propertyId
WHERE a.agentId = 1

returning all rows in table 1, when join most recent record in table 2

I've the following code, which pulls out the most recent row from a table called wwlassessments.
It works, but what I'm trying to do is show all the rows matching the WHERE criteria in table, regardless of whether there's an entry in the wwlassessments table.
I've tried changing the 2nd JOIN to a LEFT JOIN, but this just provides thousands of inaccurate results.
I'm sure it's very simple, but I can't for the life of me work out what I need to change! Thanks in advance.
SELECT s.*,
a.*
FROM wwlstatements s
LEFT JOIN wwlassessments a ON a.id = s.id
JOIN (SELECT n.id,n.pupilID,
MAX(n.dateAchieved) AS max_achieved_date
FROM wwlassessments n
where n.pupilID='114631705547'
GROUP BY n.id) y ON y.id = a.id
AND y.max_achieved_date = a.dateAchieved
WHERE s.`category`='Reading'
ORDER BY s.`statementID` ASC

SQL - How can i get the last two characters from a column?

I have three tables.
users - user_info - districts
And I built a Inner join to get the user_id and the user_info.
Select * from users a inner join user_info b on a.id = b.user_id
But i have a column called location, inside the user_info, which returns the ID from a specific location. Just like this:
00;11
And to get the location, I have to Inner Join the user_info table, to another table called districts, because the two last characters got the ID from the district.
Thats why I would like to Inner Join all three tables, like this:
Select * from users a inner join user_info b on a.id = b.user_id inner join districts c on b.location = c.District_id
The problem is that, i want to get only the two last characters from the Location column. But i'm getting
00;11 //I would like to only get the 11
I will output everything later, using Json, and I would like to get the User info, and his location.
Is it possible to "substring" a column in SQL?
Thanks.
You could use SUBSTRING_INDEX in MySQL to get the string after the semi-colon
SELECT SUBSTRING_INDEX('00;11', ';', 2);
Or in your case:
Select SUBSTRING_INDEX(Location, ';', 2),* from users a inner join user_info b on a.id = b.user_id inner join districts c on b.location = c.District_id
Alternatively you can take the last two characters of your string using RIGHT
SELECT RIGHT('00;11', 2);
Yes, it is. See: MySQL String Functions
mysql> SELECT RIGHT('foobarbar', 4);
-> 'rbar'
For further information, please see:
SUBSTR
RIGHT

mySQL- how to show all contact and their status by using INNER JOIN

This is my company Table
CompanyID, CompanyName
This is my Contact Table
ContactID, ContactName, CompanyID
This is my Report Table
ReportID, ReportName
This is my ReportContact Table, Many to Many Relationship
ContactID, ReportID
I want to return all ALL my CONTACTID of 1 company, include those who are not assign to any report, I also want to return the reportID that are assign to different contacts
1 contacts can be assign to many reports
1 reports can consist of many contacts
My current SQL CODE only manage to get the 2 contactID in the ReportContactTable
SELECT rc.ContactID, rc.ReportID from contact c INNER JOIN Reportcontact rc on c.ContactID = rc.ContactID Where CompanyID=1
how can Return all the contact include those not in the reportcontact table, but get the reportID at the same times?
INNER JOIN filters out those rows that are not in ReportContact. Try to use LEFT JOIN if you want all contacts from contact table.
SELECT rc.ContactID, rc.ReportID
FROM contact c LEFT JOIN Reportcontact rc
ON c.ContactID = rc.ContactID
WHERE CompanyID = 1
I'm not 100% sure I understand what you are trying to do, but if you want all rows from the contact table then you need to use an OUTER rather than INNER join.
Try changing the word INNER to LEFT.
An INNER join ensures that rows that satisfy the join condition appear in both tables.
An OUTER join says "show me all the rows in one table, plus those that satisfy the join condition from the other table". Which table shows all the rows depends on the use of the keywords LEFT and right
a left join b on a.id = b.id will show all rows from table a plus those from b which satisfy the join condition
a right join b on a.id = b.id will show all rows from table b plus those from a which satisfy the join condition

How to match this using SQL?

I am using the 3 following tables:
First table
id
response
Second table
responseid
patientid
Third table
patientid
The relationship between first and second table is on id and responceid.
The relationship between third and second is on patientid.
Now I need to retrieve values from these tables like all values from first and third tables with the help of matching with patientid from second and 3rd table.
How can I do this?
Basically if all of the columns that defines their relationship are not nullable, then INNER JOIN will suffice. But if they are nullable and you still want to display all records from firstTB, you need to use LEFT JOIN instead of INNER JOIN.
SELECT a.*, b.*, c.*
FROM firstTB a
INNER JOIN secondTB b
ON a.ID = b.responceID
INNER JOIN thirdTB c
ON b.patientID = c.patientID
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
You're probably looking for INNER JOIN or JOIN in general:
SELECT
response.id,
response.responce,
patient.patientid
FROM
`response_table` as `response`
INNER JOIN
`relation_table` as `relation`
ON
relation.responceid = response.id
INNER JOIN
`patient_table` as `patient`
ON
relation.patientid = patient.patientid
try
SELECT first.*
, third.*
FROM first
INNER JOIN second ON ( second.responseid = first.id )
INNER JOIN third ON ( third.patientid = second.patientid )
;
honestly, and no insult intended, if you have difficulties in coming up with queries like this one on your own, consider some training on db basics and db development, the sooner the better (just hoping i haven't blundered myself ... ;-)).