Click here to Skip to main content
15,879,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to get an output username, email, statename, districtname but getting an error

am writing query for inner joins.

What I have tried:

select 
userprofiledetails.username, userprofiledetails.email,
state_info.statename,
districtinfo.districtname
from userprofiledetails 
inner join state_info
on userprofiledetails.stateid=state_info.stateid
inner join districtinfo on 
userprofiledetails.districtid=districtinfo.districtname
Posted
Updated 22-Jan-23 18:02pm
Comments
Sandeep Mewara 22-Jan-23 21:39pm    
You have shared SQL query but error seems to be with your programming code. It clearly states the reason to - seems your conversion is failing from varchar to int as one of the values retrieved from DB is not integer. You need to handle the usecases correctly.

I would guess field values that are either NULL or space could be first possibile culprit.

1 solution

First, we're going to rewrite the statement so it's a bit easier to read:
SQL
SELECT upd.username
     , upd.email
     , si.statename
     , di.districtname
FROM userprofiledetails AS upd
INNER JOIN state_info AS si ON upd.stateid = si.stateid
INNER JOIN districtinfo AS di ON upd.districtid = di.districtname

Now, take a look at your second INNER JOIN:
SQL
INNER JOIN districtinfo AS di ON upd.districtid = di.districtname

The error is complaining that you're trying to equate a "districtname" to a "districtid".
It's telling you that you cannot convert the name to an integer to join the records in both tables.

So what do you think that join should really look like?
SQL
INNER JOIN districtinfo AS di ON upd.districtid = di.district??
 
Share this answer
 
Comments
GKP1992 23-Jan-23 1:08am    
+5

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