SQL selection based on link field
It seems to me that the idiot is asking about this ...
Table 1: users id serial person integer username char (32) Table 2: persons id serial name char (16)
Can I run a query that returns the field name for persons by specifying the username in Users?
users 1 | 1 | larry123 persons 1 | larry 2 | curly
SQL?
select name from persons where users.person=persons.id and users.username='larry123';
with the desired return
larry
I have been doing this with two passes so far and think that maybe nested selection using join is what I need
1 | larry
0
Stephen
source
to share
1 answer
It sounds like you are asking how to make a SQL connection:
SELECT
name
FROM
users JOIN persons ON (users.person = persons.id)
WHERE
users.username = 'larry123';
this is almost the request you wrote. All you were missing was an offer to join. You can also do it like this:
SELECT name
FROM users, persons
WHERE
users.person = persons.id
AND users.username = 'larry123';
I suggest finding a well-written introduction to SQL.
+5
source to share