I would like to create a variable which stores multiple strings, but nothing I have tried seems to work. I have a block of code (below) which works if I explicitly write out the three strings, but not when I pass it a variable (I am calling it @a) which I think should store the same exact strings:
Here is the block of code in question for reference, this does what I want when I write out the 3 strings ('ENSG00000120860','ENSG00000197894','ENSG00000273425'):
SELECT
ensp2, COUNT(DISTINCT (ensembl_gene_id))
FROM
(SELECT
s . *, g.ensembl_gene_id, g.associated_gene_name
FROM
blang.string s
JOIN natashal.ensp_to_ensg_and_gene_symbol g ON g.ensembl_protein_id = s.ensp1
ORDER BY ensembl_gene_id) x
WHERE
ensembl_gene_id IN ('ENSG00000120860','ENSG00000197894','ENSG00000273425')
GROUP BY ensp2
ORDER BY COUNT(DISTINCT (ensembl_gene_id)) DESC
But, I would like to set ('ENSG00000120860','ENSG00000197894','ENSG00000273425') equal to a variable since these are going to change. I don't type these by hand, but rather list them from a column of results I get. I tried to do this:
set @a = (SELECT GROUP_CONCAT(QUOTE(ensembl_gene_id) SEPARATOR ',')
FROM (SELECT DISTINCT(ensembl_gene_id) FROM natashal.ensp_to_ensg_and_gene_symbol ORDER BY RAND() LIMIT 8) x)
And the difficulty is that @a LOOKS like it should store my 3 strings well, but fails to do so:
SELECT @a
>'ENSG00000120860','ENSG00000197894','ENSG00000273425'
And I can even put parentheses around it like this:
SELECT CONCAT('(',@a,')')
>('ENSG00000120860','ENSG00000197894','ENSG00000273425')
Which looks like it works, but then when I try to pass this reference to my block of code above, like this:
SELECT
ensp2, COUNT(DISTINCT (ensembl_gene_id))
FROM
(SELECT
s . *, g.ensembl_gene_id, g.associated_gene_name
FROM
blang.string s
JOIN natashal.ensp_to_ensg_and_gene_symbol g ON g.ensembl_protein_id = s.ensp1
ORDER BY ensembl_gene_id) x
WHERE
ensembl_gene_id IN (SELECT @a)
GROUP BY ensp2
ORDER BY COUNT(DISTINCT (ensembl_gene_id)) DESC
I don't get the results I want, nothing is returned (and no error message is returned either). MySQL seems to think it's an empty array, or maybe somehow it can't match the strings to values in the table?
Thanks in advance, any help would be appreciated.