SQL query with mysql variable in a similar statement?

I want to select / check a value in a text column based on another column value, e.g.

SELECT tableA.randomID, wp_posts.post_content, wp_posts.ID
FROM tableA, wp_posts 
WHERE wp_posts.post_content LIKE '%tableA.randomID%' 
AND tableA.randomID = '110507B2VU'

But this did not work, how to set the LIKE statement

This does not work:

SELECT tableA.randomID, wp_posts.post_content, wp_posts.ID
FROM tableA, wp_posts 
WHERE wp_posts.post_content LIKE '%110507B2VU%'
+4
source share
3 answers

When you enclose something in quotation marks, it is taken as a literal value, so in the first query - where you put it LIKE '%tableA.randomID%'-. MySQL is actually treating what's in the line

Without quotes, it will take on meaning - that is:

WHERE something LIKE tableA.randomID

This actually compares "something" with the value of tableA.randomID, and not with a literal string.

To then include your% wildcards to make your LIKE expression different from the 'equal' comparisons, try the CONCAT () function.

WHERE something LIKE CONCAT("%",tableA.randomID,"%")

, tableA.randomID . 'banana' :

WHERE something LIKE '%banana%'

, : -)

+13

concat()

like concat('%',variable_name,'%')

SELECT tableA.randomID, wp_posts.post_content, wp_posts.ID
FROM tableA, wp_posts 
WHERE wp_posts.post_content LIKE CONCAT('%',tableA.randomID,'%')
AND tableA.randomID = '110507B2VU'
+2
    k = input("enter part of username known")
    sql1 = "SELECT name FROM studs WHERE name LIKE concat('%',k,'%') "
    val2 = (k,)
    mycursor.execute(sql1, val2)
    myresult = mycursor.fetchall()

this does not work !!!!

-1
source

All Articles