Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have three tables

Branch in user_branch is basically locations supposedly A,B,C
Account type in user_acct_type is account type may be D,E,F,G,H

user_branch

----------------------------------
Sr-no   |   Username  |  Branch  |
----------------------------------
1       |   User 1    |  A       |
2       |   User 1    |  B       |
3       |   User 2    |  A       |
4       |   User 2    |  C       |
---------------------------------|

user_acct_type

-----------------------------------
Sr-no   |   Username  |  Acct_type |
-----------------------------------
1       |   User 1    |  D         |
2       |   User 1    |  E         |
3       |   User 2    |  F         |
4       |   User 2    |  G         |
-----------------------------------|

cust_code

---------------------------------------------------------------------------
Sr-no   |   Customer Name  |  Customer Code |  Branch   |  Account Type   |
---------------------------------------------------------------------------
1       |   Customer 1     |  1234          |  A        |    D            |
2       |   Customer 2     |  1235          |  C        |    F            |
3       |   Customer 3     |  1236          |  B        |    G            |
4       |   Customer 4     |  1237          |  A        |    H            |
--------------------------------------------------------------------------|


Basically my requirement is that User 1 should be able to search for only those customers names whose branch is A or B(as specified in the table user_branch) and at the same time only those customer names whose account type is D or E(as specified in user_acct_type). I am not able to qrite any query such that all the conditions are satisfied in a single query. Any help is appreciated. Thanks


Edit I tried the query
SQL
select c.Cust_Name as Customer
from custcode c
inner join user_branch b on c.branch = b.branch
inner join user_acctype a on c.Cust_typeofaccount  = a.acctype
order by c.Cust_Name


Now my question is how do I filter out the results based on user session?
Posted
Updated 23-Mar-14 21:50pm
v2
Comments
Mohibur Rashid 24-Mar-14 4:03am    
Try this
SELECT a.`Sr-no`, a.`Customer Name`, a.`Customer Code` FROM `cust_code` a, `user_acct_type` b, `user_branch` c WHERE b.
`Acct_type`=a.`Acct_type ` AND c.`Branch`=a.`Branch` AND b.`Username`=c.`Username` AND c.`Username`='User 1'

By the way, bad design
Ankit Chaturvedi 24-Mar-14 4:14am    
Can you suggest me a better design? I am still confused how to design the table in the most efficient way

1 solution

SQL
SELECT a.`Sr-no`, a.`Customer Name`, a.`Customer Code` FROM `cust_code` a, `user_acct_type` b, `user_branch` c WHERE b.
`Acct_type`=a.`Acct_type ` AND c.`Branch`=a.`Branch` AND b.`Username`=c.`Username` AND c.`Username`='User 1'
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900