I'm very new to MySQL (and databases in general) and have been tasked with making a stored procedure that will accept a varchar as a parameter to be used as a keyword search from 2 columns of a table.
I'm not familiar with the syntax and searching the internet hasn't been as fruitfull as desired. This is what I came up with:
DELIMITER //
DROP PROCEDURE IF EXISTS proc_Search
CREATE PROCEDURE proc_Search (IN word varchar(100))
BEGIN
SELECT Section.Name
FROM Section inner join DetailsMats ON Section.NumSection=DetailsMats.NumSection
WHERE DetailsMats.TechName or DetailsMats.GenName LIKE %word%;
END//
Just wondering if I'm doing something disgustingly wrong here or if I'm on the right path. Thanks!
Seems should be something like this:
SELECT *
FROM MyTable
WHERE (Column1 LIKE '%keyword1%' OR Column2 LIKE '%keyword1%')
AND (Column1 LIKE '%keyword2%' OR Column2 LIKE '%keyword2%');
Try to use concat to escape the % character. Happy coding :)
DROP PROCEDURE IF EXISTS proc_Search CREATE PROCEDURE proc_Search (IN word varchar(100)) BEGIN SET @sql = CONCAT('SELECT Section.Name FROM Section inner join DetailsMats ON Section.NumSection=DetailsMats.NumSection WHERE DetailsMats.TechName or DetailsMats.GenName LIKE "',job_no, '%"'); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; END $$
I see two errors in your code:
//
after DROP PROCEDURE ...
CONCAT('%', word, '%')
to get string with word
and '%'
Corrected code:
DELIMITER //
DROP PROCEDURE IF EXISTS proc_Search // -- delimiter here
CREATE PROCEDURE proc_Search (IN word varchar(100))
BEGIN
SELECT Section.Name
FROM Section inner join DetailsMats ON Section.NumSection=DetailsMats.NumSection
WHERE DetailsMats.TechName or DetailsMats.GenName LIKE CONCAT('%', word, '%');
END
//
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.