FIELD : Search String position

SELECT FIELD('str','str1','str2','str3','str4','str');
Output is 5.
The first string is the searched string.
Returns 0 if searched string is not found.

Using our student table.
SELECT  FIELD('john',f_name,l_name) as output from student_name 
output
1
0
0
0
0
0

With WHERE condition

Get all records where any column matches search string.
SELECT * FROM `student_name` WHERE FIELD('john',f_name,l_name) >0
f_namel_nameclass
JohnDeoFour
We get one record only from our table.

WHERE condition Query

Here is the sql dump of student_name table for your use.
CREATE TABLE IF NOT EXISTS `student_name` (
  `f_name` varchar(20) DEFAULT NULL,
  `l_name` varchar(20) DEFAULT NULL,
  `class` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `student_name`
--

INSERT INTO `student_name` (`f_name`, `l_name`, `class`) VALUES
('John', 'Deo', 'Four'),
('Larry', NULL, 'Four'),
('Ronald', NULL, 'Five'),
('Garry', 'Miller', 'Five'),
(NULL, NULL, 'Five'),
(NULL, 'Ruller', NULL);

Your Rating




Google+

Post Comment This is for short comments only. Use the forum for more discussions.








HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2018 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer