0

NOTE: I am aware of SQL injection possibilities, etc. This question is theoretical.

Lets say a user queries a page on a server, getListOfPeople.php, and sends their user ID as a post parameter.

The php page contains the following (simplified pseudo code):

$UUID = $_POST ["UUID"];

"SELECT name, bio, photo FROM people WHERE NOT EXISTS (SELECT blocked_UUID FROM 'name' WHERE blocked_UUID = $UUID)"

Lets say the table people looks like this:

NAME     BIO        PHOTO
BOB      blah blah  o43987h2fdof43hr43h
SARA     blah blah  098u43jfewoijfwoiej
MARK     blah blah  re988cd9sc2h33hfddh

And the table BOB looks like this:

blocked_UUID
287364
387761
497745
329844

We want to give the people in the people table the ability to be invisible to certain users... so they simply add those unwanted users to their own table containing their list of blocked user IDs.

My problem is, in the query:

"SELECT name, bio, photo FROM people WHERE NOT EXISTS (SELECT blocked_UUID FROM 'name' WHERE blocked_UUID = $UUID)"

In the part that says, FROM 'name', I'm trying to figure out the correct syntax to use in order for that to work (querying a table where the table name is variable derived from the same query).

I would like to avoid entirely different ways of approaching the problem if possible, and stick to one single query.

13
  • You have to know the table name to get it. You could loop over the people table and get the names, then run separate queries within the loop. Seems taxing. Commented Jun 24, 2016 at 2:20
  • 1
    Having variable table names like this is horrible database design. Dynamic data should be in table data, not table and column names. Commented Jun 24, 2016 at 2:25
  • 1
    Why don't you have a single blocks table that has columns name and blocked_UUID? Commented Jun 24, 2016 at 2:25
  • 1
    I would not make separate tables based on the name in this case. I would have a table with name | blockedUUID columns or something like that and enter the name and blockedUUID so you can easily query the results you want. Commented Jun 24, 2016 at 2:29
  • 1
    @BooberBunz If you feel the need for a unique column, give it an auto-increment ID column. But you can also have a unique key on (name, blocked_UUID). Commented Jun 24, 2016 at 2:33

1 Answer 1

2

The correct way to do this is to have a single blocks table:

name    blocked_UUID
BOB     287364
BOB     387761
SARA    123456
SARA    232323
MARK    112233

Then your query would be:

SELECT name, bio, photo 
FROM people 
WHERE NOT EXISTS (
    SELECT 1 FROM blocks
    WHERE blocked_UUID = $UUID
    AND blocks.name = people.name)
Sign up to request clarification or add additional context in comments.

2 Comments

doesn't the table need to have a column where all values are unique in order to work properly? Sorry I'm new.
No, there's no such requirement, but you can always add an auto-increment ID column.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.