denormalizer
denormalizer

Reputation: 2214

How to join a CSV list to a table column

I have a table like so:

CREATE TABLE `my_table` (
    `id` INT(10) AUTO_INCREMENT,
    `code` VARCHAR(50),
    `new_table_id` INT(10)
    PRIMARY KEY (`id`)
)

And I have a CSV list of code values like so:

'23000005619',
'23000019479',
'23000019759',
'23000030169',
'23000032629'

I want to join this list with my_table.code column such that I can find the value to my_table.new_table_id so I can join that to another table.

My initial thoughts are something along the lines of this (but obviously not syntactically correct):

SELECT *
FROM my_table mt,
(
  SELECT
    '23000005619',
    '23000019479',
    '23000019759',
    '23000030169',
    '23000032629'
) as ml
WHERE mt.`code` = m1.xx

Any help would be greatly appreciated. Thanks

UPDATE

I would like to avoid temp tables where possible

Upvotes: 0

Views: 115

Answers (1)

denormalizer
denormalizer

Reputation: 2214

I think I made life harder for myself - this is all that I needed:

SELECT mt.code, mt.new_table_id
FROM my_table mt
WHERE mt.code IN
(
  '23000005619',
  '23000019479',
  '23000019759',
  '23000030169',
  '23000032629'
)

I might have thrown people off by saying i wanted a join when in fact no join was required.

Upvotes: 1

Related Questions