0

Assign a string to a defined variable isWeekdays,

set @isWeekdays='calendar.monday=1 AND calendar.tuesday=1 AND calendar.wednesday=1 AND calendar.thursday=1 AND calendar.friday=1';

I expect that the variable isWeekdays can be replaced with the string while executing a query, for instance,

SELECT * FROM calendar WHERE @isWeekdays;

-- expect to    
SELECT * FROM calendar WHERE calendar.monday=1 AND calendar.tuesday=1 AND calendar.wednesday=1 AND calendar.thursday=1 AND calendar.friday=1;

However, it does not take effect.

5
  • 1
    Have a look at dynamic queries in MySql.i.e. stackoverflow.com/questions/23178816/… Commented Jun 17, 2015 at 20:52
  • 1
    mysql treats variable contents as opaque blobs. what you're doing is executed as the equivalent of select ... where 'solidstring'. if you want the variable's contents to be evaluated/executed as part of the query, you have to use dynamic sql: build a new query string, add the variable's contents, execute the new string. Commented Jun 17, 2015 at 21:04
  • seriously though, why even do this? are you just bored? need a hobby? Commented Jun 17, 2015 at 21:06
  • @NotoriousPet0 because I use @isWeekdays many times in my sql queries. Now, I know dynamic queries can make it. Commented Jun 17, 2015 at 21:58
  • 1
    Why not just make it a variable of whatever scripting language your using? Commented Jun 17, 2015 at 21:59

2 Answers 2

2

@isWeekdays will evaluate as numeric in query, you must use a statement and it will work as dynamic query:

-- setting the @isWeekdays variable
set @isWeekdays='calendar.monday=1 AND calendar.tuesday=1 
     AND calendar.wednesday=1 AND calendar.thursday=1 
      AND calendar.friday=1';


-- using the @isWeekdays variable in dynamic query
set @sql = concat('SELECT * FROM calendar WHERE ',@isWeekdays);
prepare stmt1 FROM @sql; 
execute stmt1; 
-- you muyst close the prepared statement
deallocate prepare stmt1; 

You can change @isWeekdays in the same session and open the statement again.

That's it.

Sign up to request clarification or add additional context in comments.

Comments

0

With the help of @Notorious Pet0, compared with dynamic queries, using script language seems better. The key source codes (using format strings in python) is as follows.

#!/usr/bin/env python
import MySQLdb
import codecs  #for charset utf-8

## connect to MySQL
db = MySQLdb.connect(host="localhost",
        user="root",
        passwd="Jelline", # password
        db="gtfs")  #name of the data base

## create a Cursor object to execute sql queries
cur = db.cursor()

## execute queries
isWeekdays = 'calendar.monday=1 AND calendar.tuesday=1 AND calendar.wednesday=1 AND calendar.thursday=1 AND calendar.friday=1'
sql = 'SELECT * FROM calendar WHERE {isWeekdays}'.format(isWeekdays=isWeekdays)

cur.execute(sql)

## write to a file
with open('test.txt', 'w') as fp : #fp is a file object
    for row in cur.fetchall() :
        s = '\t'.join(str(item) for item in row)
        fp.write(s + '\n')

## clean up
fp.close()
cur.close()
db.close()

Comments

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.