MySQL Forums
Forum List  »  Newbie

Re: How to get column/field names with select
Posted by: Felix Geerinckx
Date: August 11, 2005 07:15AM

dump3 wrote:

> If I have a large select looking for matching information across MANY columns, is there
> a way I can display WHICH field or column names the query did or did not match on?

USE test;
DROP TABLE IF EXISTS foo;
CREATE TABLE foo (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
field1 CHAR(20),
field2 CHAR(20)
);

INSERT INTO foo (field1, field2) VALUES
('abc', '--abc--'),
('xyz', 'abc'),
('xyz', 'xyz');


SELECT
id,
CONCAT_WS(', ',
IF(field1 LIKE '%abc%', 'field1', NULL),
IF(field2 LIKE '%abc%', 'field2', NULL)
) AS 'Found in'
FROM foo
WHERE
field1 LIKE '%abc%' OR
field2 LIKE '%abc%';

--
felix
Please use BBCode to format your messages in this forum.

Options: ReplyQuote


Subject
Written By
Posted
Re: How to get column/field names with select
August 11, 2005 07:15AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.