简体   繁体   中英

Select a subset of one list using another list in MySQL

I'm really new to mysql and I have a bit of complicated question. I have a table of firstname, lastname, username among other information. I queried my table for all usernames that have a specific attribute (Eg admins) and put my results in a temporary table. I now would like to use my subset (admin) list and query against my original list to learn what other accounts these individuals might have. Where I struggle is to formulate my query to use the information of the subset to look for occurrences in the overall list (table). I tried something like this:

SELECT * FROM fulllist WHERE lastname LIKE lastname FROM adminlist;

But that seems not to work. I lack the understanding what functionality I should use to get this done. I would appreciate some pointers on how I can continue to figure this out and query my list appropriately. Thank you. John.

There are several ways to do this, here's one option using in :

select * 
from fulllist 
where lastname in (select lastname from adminlist);

Just a note, you don't really need a temporary table for this sort of thing. Just use the query you have in the subquery above. Something like:

select * 
from fulllist 
where lastname in (
    select lastname 
    from fulllist
    where someattribute = 'Admin');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM