Skip to content
Advertisement

Loop through SQL query using variable from another table

I have two tables file & users, I want to see the file info for each user for C:Users%USERNAME%Documents

So e.g. this would get the info from ‘example’ documents:

But the username is coming from the users

returns

Alternatively, there’s:

Which provides the first part of the path, but still can’t get to join ‘Documents’ to end of query and also run the file query.

So, how do I loop through the each of the usernames.

I’ve tried modifying but neither table can be modified

Advertisement

Answer

This is a great opportunity to use a JOIN query:

When you run this query, osquery will first generate the list of users, then substitute the username into the path provided to the file table.

JOIN is a really powerful way to combine the results of various tables, and it’s well worth taking some time to experiment and learn how to use this power.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement