Error Unknown column in 'where clause' matlab using row array

I want to read a column from a database table, and for each row I read, I read several rows from another table. the elements I read from both tables are rows, my code is:

query1 = 'select col1 from MyDb.table1 order by col1'; 
[x1]= mysql(query1);
     for i=1:size(x1)
         my=char(x1(i,1));    
         query2=sprintf('%s%s%s%s',['select col1 from MyDb.table2 where col1=', my, ' or col2=', my]);
         [x2]=mysql(query2); 
     end

when I run the code, I get this error: Unknown column "khlkuu" in 'where clause', khlkuu is the row in the first table. I know that this error is due to the fact that it does not surround the fang with a quote, but I do not know how to do it. any ideas?

+3
source share
1 answer

To get quotes in a string, you need to use double quotes. In addition, the call sprintfseems unnecessary.

my = 'something';
query2= ['select col1 from MyDb.table2 where col1=''', my, ''' or col2=''', my,'''']

query2 =
select col1 from MyDb.table2 where col1='something' or col2='something'
+4

Source: https://habr.com/ru/post/1769850/


All Articles