3

I want to read a column from a table of data base and for every row that I have read, read some rows from another table. the elements that I read from both tables are Strings, 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 a row in first table. I know that this error is because of not surrounding khlkuu with quotation , but I do not know how to do so. any Ideas?

1

1 Answer 1

4

To get quotes in a string, you need to use double-quotes. Also, the call to sprintf seems 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'
Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much for your answer Jonas, you made it so easier :)

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.