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.

SparkAndShine
  • 17,001
  • 22
  • 90
  • 134
  • 1
    Have a look at dynamic queries in MySql.i.e. http://stackoverflow.com/questions/23178816/mysql-dynamic-query-in-stored-procedure – artm Jun 17 '15 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. – Marc B Jun 17 '15 at 21:04
  • seriously though, why even do this? are you just bored? need a hobby? – Tech Savant Jun 17 '15 at 21:06
  • @NotoriousPet0 because I use `@isWeekdays` many times in my sql queries. Now, I know `dynamic queries` can make it. – SparkAndShine Jun 17 '15 at 21:58
  • 1
    Why not just make it a variable of whatever scripting language your using? – Tech Savant Jun 17 '15 at 21:59

2 Answers2

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.

Ivan Cachicatari
  • 4,212
  • 2
  • 21
  • 41
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()
SparkAndShine
  • 17,001
  • 22
  • 90
  • 134