Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL select everything with arbitrary IN clause

Tags:

sql

mysql

This will sound silly, but trust me it is for a good (i.e. over-engineered) cause.

Is it possible to write a SQL query using an IN clause which selects everything in that table without knowing anything about the table? Keep in mind this would mean you can't use a subquery that references the table.

In other words I would like to find a statement to replace "SOMETHING" in the following query:

SELECT * FROM table_a WHERE table_a.id IN (SOMETHING)

so that the results are identical to:

SELECT * FROM table_a

by doing nothing beyond changing the value of "SOMETHING"


To satisfy the curious I'll share the reason for the question.

1) I have a FactoryObject abstract class which grants all models that extend it some glorious factory method magic using two template methods: getData() and load()

2) Models must implement the template methods. getData is a static method that accepts ID constraints, pulls rows from the database, and returns a set of associative arrays. load is not static, accepts an associative array, and populates the object based on that array.

3) The non-abstract part of FactoryObject implements a getObject() and a getObjects() method. These call getData, create objects, and loads() the array responses from getData to create and return populated objects.

getObjects() requires ID constraints as an input, either in the form of a list or in the form of a subquery, which are then passed to getData(). I wanted to make it possible to pass in no ID constraints to get all objects.

The problem is that only the models know about their tables. getObjects() is implemented at a higher level and so it doesn't know what to pass getData(), unless there was a universal "return everything" clause for IN.

There are other solutions. I can modify the API to require getData to accept a special parameter and return everything, or I can implement a static getAll[ModelName]s() method at the model level which calls:

static function getAllModelObjects() {
    return getObjects("select [model].id from [model]");
}

This is reasonable and may fit the architecture anyway, but I was curious so I thought I would ask!

like image 405
slifty Avatar asked Oct 16 '25 05:10

slifty


2 Answers

Works on SQL Server:

SELECT * FROM table_a WHERE table_a.id IN (table_a.id)
like image 71
fankt Avatar answered Oct 18 '25 21:10

fankt


Okay, I hate saying no so I had to come up with another solution for you.

Since mysql is opensource you can get the source and incorporate a new feature that understands the infinity symbol. Then you just need to get the mysql community to buy into the usefulness of this feature (steer the conversation away from security as much as possible in your attempts to do so), and then get your company to upgrade their dbms to the new version once this feature has been implemented.

Problem solved.

like image 29
Brandon Moore Avatar answered Oct 18 '25 21:10

Brandon Moore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!