Sorry if this seems simple. Either I'm not asking the right question, or understanding the answers I've found so far.
i need to join columns
(Ex. tbl_lockers has Employee and tbl_employee has Name column)
I need to join the same data from tbl_employee.Name to tbl_lockers.Employee
Any thoughts? Thanks.
I'm a newbie in SQL.
Try using:
SELECT * FROM
tbl_employee, tbl_lockers
WHERE tbl_employee.Name = tbl_lockers.Employee
Specifing the exact columns may be needed if both tables contain columns with the same names.
To show all employees who have a locker (ie. all criteria match), do this...
SELECT e.*, l.*
FROM tbl_employee e
INNER JOIN tbl_lockers l ON l.Employee = e.Name
To show all employees whether they have a locker or not, change the JOIN Criteria to...
LEFT JOIN tbl_lockers l ON l.Employee = e.Name
To show all lockers whether they have a employee or not, change JOIN Criteria to...
RIGHT JOIN tbl_lockers l ON l.Employee = e.Name
Related
Edit
I would rather explicitly state my problem I'm facing rather than assuming the approach. There could be an easiest solution to my problem.
I need to select records by joining two different table based on the result from another table. And I have to use different joins based on the result from the first table.
If a particular record is present in the first table, I have two use inner join the first table whereas if it's not present, then I have to left join.
bool recordPresent = select exists (select * from firstTable where access_id = 13) as access
if (recordPresent)
results = select * from secondTable s left join firstTable f on f.access_id = s.access_id where f.access_id is null order by access_id
else
results = select * from secondTable s inner join firstTable f on f.access_id = s.access_id
Here is my query:
ALTER PROCEDURE sp_logdetails
(
#bookid INT
)
AS
BEGIN
SELECT *
FROM book_lending
LEFT OUTER JOIN student ON student.id = book_lending.id
WHERE bookid = #bookid
END
When i execute above query, it shows only book_lending data, not the student data. Here is my screenshot
And it is my student table data screenshot:
May i know, how to get student data in which particular bookid. I used to set foreign key for book_lending id to student id.
Can anyone guide me to fix this?
Thanks,
Select the specific columns from joined table in your select list. Here bl and s is table alias for better redability. You need to select the columns you want and include them to your select list. below query will select all columns from both tables.
SELECT bl.*, s.*
FROM book_lending bl
LEFT OUTER JOIN student s ON s.id = bl.id
AND bl.bookid = #bookid
You got the fields mixed up in the join:
LEFT OUTER JOIN student ON student.id = book_lending.id
You try to match a student to book lending by ID of both - which is wrong.
Change the code to this and you will start getting results back:
SELECT *
FROM book_lending
LEFT OUTER JOIN student ON student.id = book_lending.studentid
WHERE bookid = #bookid
Please Help I got a problem with join in my sqlite database,In that I 'm having two tables like userdetails and usertransaction tables,
userdetails having 5 records like userids 1,2,3,4,5 with their details
and 2nd table usertransaction having only 1 and 2 records.
I want to take userdetails with taht user trasactions sum...
if no transactions simply replace zero and remaing details.
query like this...
Select tblfarmers.farmerid,
tblfarmers.farmerName,
tblfarmers.mobileNumber,
tblfarmers.Address,
SUM(IfNULL(tblFarmerAdvanceDetails.presentadvance, 0)) as presentadvance
from tblfarmers
left join tblFarmerAdvanceDetails on tblFarmerAdvanceDetails.farmerid=tblfarmers.farmerid
where tblfarmers.farmerid='2'
and tblFarmerAdvanceDetails.isactive='true'
ORDER BY tblfarmers.farmerid
You are enforcing a condition in your left joined table that only rows that are actually joined can fulfill:
tblFarmerAdvanceDetails.isactive='true'
To avoid this you should remove this condition from the WHERE part and add it to the join condition.
SELECT tblfarmers.farmerid,
tblfarmers.farmerName,
tblfarmers.mobileNumber,
tblfarmers.Address,
SUM(IfNULL(tblFarmerAdvanceDetails.presentadvance, 0)) as presentadvance
FROM tblfarmers
LEFT JOIN tblFarmerAdvanceDetails ON (tblFarmerAdvanceDetails.farmerid = tblfarmers.farmerid AND tblFarmerAdvanceDetails.isactive = 'true')
WHERE tblfarmers.farmerid='2'
ORDER BY tblfarmers.farmerid
I have the following DB structure:
tbl_record(_id,_id_user,...)
tbl_photo(_id,_id_record,...)
tbl_note(_id,_id_record,...)
When listing the records of a specific user while counting the number of photos a record has, I use the following query, which works fine:
SELECT tbl_record._id, COUNT(tbl_photo._id_record) AS photo_count FROM tbl_record
LEFT OUTER JOIN tbl_photo ON tbl_record._id=tbl_photo._id_record
WHERE tbl_record._id_user=? GROUP BY tbl_record._id;
Now, I'd like to do the same as above, but also count the number of notes a record has:
SELECT tbl_record._id, COUNT(tbl_photo._id_record) AS photo_count, COUNT(tbl_note._id_record) AS note_count FROM tbl_record
LEFT OUTER JOIN tbl_photo ON tbl_record._id=tbl_photo._id_record
LEFT OUTER JOIN tbl_note ON tbl_record._id=tbl_note._id_record
WHERE tbl_record._id_user=? GROUP BY tbl_record._id;
The count of the 2nd query does not work properly when a record has >0 photos & >0 notes, e.g. 3 photos & 5 photos which results in a count of 15 (3*5) for each.
Any idea how to make the 2nd query return the proper counts?
Thanks!!
You might be able to filter out duplicates by using COUNT(DISTINCT some_id), but this would be inefficient.
Better use correlated subqueries:
SELECT _id,
(SELECT COUNT(*)
FROM tbl_photo
WHERE _id_record = tbl_record._id
) AS photo_count,
(SELECT COUNT(*)
FROM tbl_note
WHERE _id_record = tbl_record._id
) AS note_count
FROM tbl_record
WHERE _id_user = ?
I am using it but no value i found...I think there is mistake in this query....Actually I want to know how to use multiple sum, multiplication etc using mutiple tables in sqlite
SELECT
dhid, dprice, dname,
SUM(dmilk) AS totalmilk,
dprice*SUM(dmilk) AS totalmilkamt,
SUM(ghee) AS toalghee,
SUM(ghee*gheeprice) AS totalgheeamt,
SUM(ghee*gheeprice)+dprice*SUM(dmilk) AS totals,
SUM(cashamount) AS totalcash,
SUM(ghee*gheeprice)+dprice*SUM(dmilk)-SUM(cashamount) AS balance
FROM
( SELECT *
FROM costumer
LEFT OUTER JOIN salesdata
ON costumer.dhid=salesdata.ddhid
LEFT OUTER JOIN cashdata
ON salesdata.ddhid=cashdata.uid
AND utype='costumer')
WHERE dmonth='$mikdatem'
AND dyear='$mikdatey'
AND dhid='$dhid'
ORDER BY dhid ASC
Your select above will not help us because we don't have the underlying data to get an idea what you wish to do.
So the generalistic answer is this:
when using grouping-functions (SUM/COUNT...) you always require some form of "GROUP BY" to columns not used in those group-functions.
Example given:
SELECT name, sum(dmilk)
FROM milk_entry
GROUP BY name