SQLALchemy query all() but get specific fields in list - sqlalchemy

I have this select statement where I get all ids for a particular name:
select name from table1 where id=100;
var1=DBSession.query(table1).filter(table1.id==100).first().name
This returns me with just the first name value for the id 100. However, My query results multiple names and I have to use the all clause like this
varlist=DBSession.query(table1).filter(table1.id==100).all()
Then I am trying to access name from varlist like
for i in varlist:
otherlist.append(varlist.name)
because this query doesnt put all names in the list:
varlist=DBSession.query(table1).filter(table1.id==100).all().name
Can someone tell me how to get names from .all() directly into "varlist" without using an intermediate list?

The all() method is simply syntactic sugar for calling list on the query. So since you want a single column and not the entity, something like this should work,
names = [name for name, in DBSession.query(table1.name).filter(table1.id == 100)]

Slightly modifying #Jared's result to make this look better with .all():
namelist=[n.name for n in DBSession.query(table1).filter(table1.id==100).all()] also works. Thank you Jared.

Related

how to pass a database column as a parameter in a user defined function?

i have a code in sql for string comparison which takes two parameters as input works upon it and returns a result. both of the parameters are words, i want to change the parameter from a single word to a database column. how do i do that?
say for example in java its like storing the data in an array and than passing the whole array. can something like this be done in sql?
You can use the Select query for passing each value of a particular column from the table into your function.
like this,
SELECT compare_city_name('baroda',t.cityname) from tablename as t
In this query, you pass all cities name from cityname column to the function compare_city_name one by one.
Pass it as a VARCHAR, then build the query with "prepare" and "execute" it.

Making a SQL query via regex matching

Is it possible to make a SQL query by matching a pattern? I know SQL allows for wildcards but I dont think they fit my use case.
Suppose I have a table that contains the following record (represented here in JSON):
Table Name: url_stuff
{
id: 2
path: \/user\/(.*)\/
value: "I am the user path"
}
Then suppose I had the following string representing a URL path:
/user/gandalfthewhite
I would like to make a query that returns this record.
SELECT * FROM url_stuff WHERE path LIKE '/user/gandalfthewhite'
Obviously this wont work, but perhaps there is some other way to use SQL such that /user/gandalfthewhite matches \/user\/(.*)\/ as it would with regex and return the above record.
One solution is obviously to grab all records from the database and search via regex after the fact, but this would not be scaleable for a large number of records. I would ideally be able to grab all matching records with a query directly.
If I understand correctly, you can just use regexp:
SELECT *
FROM url_stuff
WHERE '/user/gandalfthewhite' REGEXP url

SQL - If input string is empty return all rows , otherwise try to match

I have table matches with these columns:
|sport|region|country|league
If the input string for sport is empty I want to return everything and don't bother with matching region, country etc.
If sport is not empty then find rows with matched sports and proceed to region and do the same thing.
Is this possible to do in SQL? I know I can filter this out in PHP and then run different SQL queries.
Try this
WHERE (sport_param IS NULL OR sport_column = sport_param)
You might want to use the LIKE operator or consider case-insensitive checking instead of simply comparing the exact sport.
Take a look at this
... you can basically make an if statement in SQL to match whether sport is empty or not and depending on that execute two different queries.

MySql extract data from a database based on two columns

I Am trying to create/run an my sql query in such a way that the sql selects data based a some conditions from Column 1 (USER) but at the same time Excludes some data, based on some conditions from column 2 (ADDRESS)
E.g.:
SELECT ADDRESS,USER
FROM Data1.Table1
WHERE FIELD(USER,'%AMIT%','%JOHN%','%SANDEEP%','%WARNE%')
AND ORIGINATING_ADDRESS NOT LIKE 'MUMBAI','CHINA','PAKISTAN'
This is giving error.Can some one please help ?
Use NOT IN to discard list of values from select. Considering that you want to discard when there is exact match
ORIGINATING_ADDRESS NOT IN ('MUMBAI','CHINA','PAKISTAN')
When you want to use pattern search and discard the use this
ORIGINATING_ADDRESS NOT LIKE '%MUMBAI%' OR
ORIGINATING_ADDRESS NOT LIKE '%CHINA%' OR
ORIGINATING_ADDRESS NOT LIKE '%PAKISTAN%'
For a set of values, use NOT IN, instead of NOT LIKE.
You might find regular expressions simpler for this purpose:
SELECT ADDRESS,USER
FROM Data1.Table1
WHERE USER REGEXP 'AMIT|JOHN|SANDEEP|WARNE' AND
ORIGINATING_ADDRESS NOT REGEXP 'MUMBAI|CHINA|PAKISTAN';

MySQL Select Row Where Column Contains a Value

I have tried using 'LIKE' but it runs into problems which i will explain below.
i have a string column that looks like any of these.
"1010, 2020, 3030"
"1010"
""
I want to be able to see if this string contains a single ID. e.g 2020. if it does then return the row. I tried using like but if the id is 20 it will return the row because 2020 contains 20.
Selecting the entire db and then using a delimiter to go through all the strings will take far too much time. Is it possible to implement this?
This is why you don't store multiple values in a single field. Because your bad design, this is the query structure you'll have to use EVERY SINGLE TIME to compensate for it:
WHERE
foo = 2020 // exact match, only value in field
OR foo LIKE '2020,%' // value is at start of field
OR foo LIKE '%,2020,%' // value is somewhere in the middle of the field
OR foo LIKE '%,2020' // value is at the end of the field
Or you could have had a properly normalized design, and just done
WHERE childtable.foo = 2020
and be done with it.
First, you should not store lists of things in string variables. SQL has a very nice data structure for lists. It is called a table. Each row in such a table would have an id and one value from the list.
That said, sometimes you are stuck with data like this. In that case, you can use find_in-set():
where find_in_set('20', replace(stringcolumn, ', ', ',')) > 0;
You can also do the logic with like, but MySQL has the convenient built-in function for this.
EDIT:
If you want to do this with like:
where concat(',', stringcolumn, ',') like '%,20,%'
Note that the delimiters "protect" the values, so 20 is not confused with 2020.