← Back to Kurakani

SQL statement help needed

The opening post of this discussion is no longer available. The replies are shown below.

15 Replies
virusno1 Nov 16, 2015 1:04 PM
First of all, I am not sure why there is an emailaddress in Address table. That's just bad table design. That just violates normal form of DB design and you will end up storing tons of redundant data. If possible redesign your table like this Customers- contains customerid,First name last name, emailaddress Address- Contains addressid,City State, zipcode customeraddressBridge- customerid and addressid Then Select C.FirstName, C.LastName,A.City,A.State,A.Zipcode From Customer C join customeraddressBridge CAB on CAB.CustomerID=C.CustomerID join Address A on A.AddressID=CAB.AddressID If this is not what you are looking for then give some example what you are looking for. It's not very clear from your question.
virusno1 Nov 16, 2015 1:11 PM
Wait a second, Does your Address table really contains EmailAddress field? I hope not. If that's the case I really don't know what are you trying to do because you completely screwed up the entire query. Basically what you are doing is you are not joining two tables, you are joining a table with same table with Email address. So of course you will get all the emailaddress from customers. I am pretty much sure you didn't build those table according to your level of writing queries. Do you really understand the join? If not stop what you are doing and study join first because it looks like what you are asking is completely messed up.
virusno1 Nov 16, 2015 1:30 PM
lol, ok Then why are you joining a table which can not be joined? Now I know what's your problem is. There must be a table which has a mapping between your customer table and address table in your database. Find that table and above query i.e Select C.FirstName, C.LastName,A.City,A.State,A.Zipcode From Customer C join customeraddressBridge CAB on CAB.CustomerID=C.CustomerID join Address A on A.AddressID=CAB.AddressID You are completely missing key table which is a bridge or mapping table
bairaghi Nov 16, 2015 3:08 PM
@cp21 if u need IT help, u know where to turn. DANG !!!
bairaghi Nov 16, 2015 3:34 PM
@RK LOL
bairaghi Nov 16, 2015 3:59 PM
LOL no offense, was just joking.
prankster Nov 17, 2015 10:21 AM
Lets, see if you are trying to do following, Customer_table -------------------- Customer_Id | Customer_Name | Customer_City | Customer_State Email_Id_table ------------------ Customer_Id | Customer_Email_Id Select C.Customer_Id, C.Customer_Name, C.Customer_City, C.Customer_State, E.Customer_Email_id From Customer_table C Join Email_Id_talbe E on C.Customer_Id = E.Customer_Id Here is the sqlfiddle, http://sqlfiddle.com/#!6/655f9...
SolveSQL Nov 17, 2015 10:39 AM
Try this... SELECT Name,City,State--,(add more cols as needed) FROM ADDRESS a --(assuming thats your table name) JOIN customer c ON a.customerid= c.customerid --(assuming customer is the table name and customerid is the common column) WHERE c.emailaddress = 'abcde@abc.com' --(assuming emailaddress is the column that has email for the customer) Last edited: 17-Nov-15 10:40 AM
NepaliBhai Nov 17, 2015 11:12 AM
Last edited: 17-Nov-15 11:53 AM
NepaliBhai Nov 17, 2015 11:12 AM
Last edited: 17-Nov-15 11:54 AM
SolveSQL Nov 17, 2015 12:03 PM
"This statement doesn't return anything." does it error out....i dont think it would. replace your last line with... Where rtrim(ltrim(c.emailaddress)) = 'allen.sherwood@yahoo.com' Last edited: 17-Nov-15 12:03 PM
SolveSQL Nov 17, 2015 12:52 PM
use the trim logic in the on clause as well.
SolveSQL Nov 17, 2015 1:00 PM
you have the email address misspelled.....its "allan" not "allen"! use copy paste option...limits human error!
prankster Nov 17, 2015 1:16 PM
With small difference, http://sqlfiddle.com/#!6/433b6...
Ruben123 Nov 17, 2015 5:43 PM
Assuming you are looking for shipping address: Query 1: select concat(FirstName,' ',LastName) as customer_name, email, concat(Line1,' ',Line2, ' ',City,' ',State,' - ',ZipCode) as shipping_address, phone as phone_number from customer c left outer join address a on (c.CustomerID = a.CustomerID) and (c.ShippingAddressID = a.ShippingAddressID); Query 2: passing email as parameter. select concat(FirstName,' ',LastName) as customer_name, email, concat(Line1,' ',Line2, ' ',City,' ',State,' - ',ZipCode) as shipping_address, phone as phone_number from customer c left outer join address a on (c.CustomerID = a.CustomerID) and (c.ShippingAddressID = a.ShippingAddressID) where email = {pass_email}; Query 3: Using named query select concat(FirstName,' ',LastName) as customer_name, email, concat(Line1,' ',Line2, ' ',City,' ',State,' - ',ZipCode) as shipping_address, phone as phone_number from customer c left outer join address a on (c.CustomerID = a.CustomerID) and (c.ShippingAddressID = a.ShippingAddressID) where email = :email_id;
Login to join the conversation.

You might be interested in...

Recent Classifieds View all
Upcoming Events View all
Service Providers View all
Ask Sajha AI Assistant