How can I create a single sql command to display the statements of accounts of one of the customer using inner join? help please, thanks.
I see that your custumers are identified by the telephone number, i don't think that is a good idea, since telephone number can change quite often in your custumer table, anyway this should be the query.
SELECT SA.* FROM STATAMENT_OF_ACCOUNT_TBL SA
JOIN OFFICIAL_RECEIP_TBL R ON SA.STATEMENT_ACC_NO=R.STATEMENT_ACC_NO
JOIN CUSTUMER_TBL C ON C.CUS_TEL_NO=R.CUS_TEL_NO
WHERE C.CUS_TEL_NO='422-9418'
Ah, and there should not be null on the keys you are trying to join or it could result in nothing as results.
Related
Thank to your help I made a view in my database called 'people' that retrieve data using three functions called 'isUserVerified', 'hasUserPicture' and 'userHobbies' from two tables called 'users' and 'user_hobbies':
SELECT
`u`.`id` AS `id`,
`isUserVerified`(`u`.`id`) AS `verification`,
`hasUserPicture`(`u`.id) AS `profile_picture`,
`userHobbies`(`h`.`user_id`) AS `hobbies`
FROM
`people`.`users` u
INNER JOIN
`people`.`user_hobbies` h
ON
`h`.`user_id` = `u`.`id`
It returns the following output:
I realise that this is because I am joining on:
`h`.`user_id` = `u`.`id`
But it is not what I want. For each user I want to run the tree function and return if they are verified, have a profile picture and a hobby. I am expecting 10 users with the relative information. Can you help? Thank you
I don't think you need to join to hobbies at all. Your functions are doing the work for you:
SELECT u.id,
isUserVerified(u.id) AS verification,
hasUserPicture(u.id) AS profile_picture,
userHobbies(u.id) AS hobbies
FROM people.users u;
Note that user-defined functions tend to slow queries down, sometimes a lot. Functions may be a good idea in some languages, but in SQL it is better to express the logic as JOINs and GROUP BYs.
Also, there is no reason to use backticks if the identifiers don't have "bad" characters. Unnecessary backticks just make the query harder to write and read.
You can replace INNER JOIN with LEFT JOIN to see all of the users, since users table is stated on the left of the JOIN keyword, and INNER looksup for the exact match in the condition. e.g. if there's no spesific user id inserted into the hobbies table, the related row is not returned by INNER JOIN.
I have 2 tables, one is setting and one is accounts
setting has columns of: isVerified, customMessage, user
accounts has columns of: id, fullName, password, address, phone
I know I have to do join but how do I get only fullName from accounts table?
I did this
SELECT isVerified, customMessage, fullName
FROM setting FULL OUTER JOIN
accounts
ON setting.user = accounts.id;
but got error near the JOIN. What's wrong?
An inner join should suffice:
SELECT s.isVerified, s.customMessage, a.fullName
FROM setting s INNER JOIN
accounts a
ON s.user = a.id;
MySQL does not support FULL OUTER JOIN. Presumably, all accounts have settings and vice versa.
Note that I introduced table aliases so the query is easier to write and to read. And, in this query, all column names specify the table they come from.
I know I have to do join but how do I get only fullName from accounts
table?
If you only want fullName only specify fullName column in your select statement.
Select fullname FROM ....
As others have pointed out MySQL doesn't support FULL OUTER JOIN so change that to simply JOIN as Gordon Linoff has mentioned above.
Normally when you do a join you either want rows that match both the tables (setting and accounts in your case). Based on the columns you've described and depending on how you've designed your schema it's either a One to One relationship between two tables or One to Many. Your case sounds like one to one as each users account will have a setting.
You're joining on s.user = a.id but I don't see you mentioned s.user is actually same as a.id? What is the user field? Perhaps you need to name this better as s.id if it's actually an id. As others have pointed out please include your actual table definition so it's easier to figure out why you get the SQL error while running your query.
Good luck.
I need to perform a SELECT query on 3 tables and i don't know if using a sub-query could be better than a LEFT JOIN since one column in some case might be missing. These are the tables:
Options (name, info...)
Owners (name, address)
Rel (idoption, idowner)
The SELECT should return all the Options with the name of the Owner inside each record but, in some case, the Option might not be connected to any Owner and the name of the Owner should be empty.
Any suggestions? Thanks in advance
A LEFT JOIN is likely the appropriate response and will probably be faster than a subquery depending on your results (it's possible that they'd compile to the same plan).
SELECT
op.name
,op.info
,...
,ow.name
,ow.address
FROM
options op
LEFT OUTER JOIN
Rel r
ON r.idoption = op.id
LEFT OUTER JOIN
owners ow
ON ow.id = r.idowner
LEFT JOIN then, it will get all the Options irregardless if there is a matching Owner or not - "This extra consideration to the left table can be thought of as special kind of preservation. Each item in the left table will show up in a MySQL result, even if there isn't a match with the other table that it is being joined to."
from: http://www.tizag.com/mysqlTutorial/mysqlleftjoin.php
A left join will be much more efficient and faster than a subquery. If you can live with NULLs for the cases where there's no match, it's the better approach.
There are 4 sql tables:
Listings(Amount, GroupKey, Key, MemberKey),
Loans(Amount, GroupKey, Key, ListingKey),
Members(City, GroupKey, Key)
Groups(GroupRank, Key, MemberKey)
Now, if one wants to find out the loans which are also listings and find the members city and GroupRank for the members in the loan table. Here, the group table contains information about grous of which members are a part of.
and also perform a select operation as given below:
select Listings.Amount, Members.City, Groups.GroupRank
from listings, loans, members, groups
where Listings.Key=Loans.ListingKey and
Members.Key=Listings.MemberKey and
Listings.GroupKey=Groups.Key
The above join is giving an incorrect result, please point out where I am going wrong.
Also I am new to SQL so please excuse the novice question.
Note: The following is just a guess what your problem is. Like others said, clearify your question.
You want to JOIN
( http://dev.mysql.com/doc/refman/5.1/de/join.html )
those tables. What you write is just another form of a join, meaning it has the same effect. But you "joined" a bit too much. To make things clearer a syntax has been invented to make things clearer and avoid such mistakes. Read more about it in the link given above.
What you want to achieve can be done like this:
SELECT
Listings.Amount, Members.City, Groups.GroupRank
FROM
Listings
INNER JOIN Groups ON Listings.GroupKey=Groups.Key
INNER JOIN Members ON Members.Key=Listings.MemberKey
You don't do a SELECT on the Loans table, you don't need it in this query.
This is the INNER JOIN which will give you a result where every row in table A has an according entry in table B. When this is not the case, you have to use the LEFT or RIGHT JOIN.
Maybe the problem is related to the join type (INNER). Try LEFT JOIN for example but Mark has right: you should clearify your question.
I would firstly change your query to use the more modern join syntax, which allows outer joins. Tr this:
select Listings.Amount, Members.City, Groups.GroupRank
from listings
left join loans on Listings.Key=Loans.ListingKey
left join members on Members.Key=Listings.MemberKey
left join groups on Listings.GroupKey=Groups.Key
and/or Loans.GroupKey=Groups.Key
and/or Members.Key=Groups.MemberKey
You may need to play with the criteria on the last join (maybe they should be "or" not "and" etc).
So I've asked a couple of questions about performing joins and have had great answers, but there's still something I'm completely stumped by.
I have 3 tables. Let us call them table-b, table-d and table-e.
Table-b and table-d share a column called p-id.
Table-e and table-b share a column called ev-id.
Table-e also has a column called date.
Table-b also has a unique id column called u-id.
I'd like to write a query which returns u-id under the following conditions:
1) Restriced to a certain value in table-e.date.
2) Where table-b.p-id does not match table-d.p-id.
I think I need to inner join table-b and and table-e on the e-id column. I then think I need to perform a left join on table-d and and table-b where p-id is null.
My problem is that I don't know the syntax of writing this query. I know how to write multiple inner joins and I know how to write a left join. How do I combine the two?
Thanks so much to everyone who is helping me out. I'm (obviously!) a newbie to databases and am struggling to get my head around it all!
You just write the joins one after the other:
SELECT b.uid
FROM b
INNER JOIN e USING(evid)
LEFT JOIN d USING(pid)
WHERE e.date = :whatever
AND d.pid IS NULL
I think it's something like this:
SELECT uid
FROM table-b
INNER JOIN table-e
ON table-b.ev_id = table-e.ev_id
WHERE table-b.p_id NOT IN (SELECT p_id from table-d)