Warning: Creating default object from empty value in /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php on line 4991

Warning: Creating default object from empty value in /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php on line 4993
Question 79: Which alternatives exist? – MySQL Question of the Day

Skip to content

By plogi in mysql questions

You want to list all rows from the mysql.help_keyword table,
in which the name contains ‘TREE’ or ‘TYPE’.
The obvious choice is:

SELECT * FROM mysql.help_keyword WHERE name like '%TREE%' or name like '%TYPE%';
+-----------------+-------+
| help_keyword_id | name  |
+-----------------+-------+
|              87 | RTREE |
|             107 | TYPE  |
|             441 | BTREE |
|             445 | TYPES |
+-----------------+-------+
4 rows in set (0.01 sec)

What other alternatives can you think of?

[Just for fun]

Tags: , ,

Comment Feed

One Response

  1. plogi8. July 2010 @ 18:31:08


    SQL is very powerful and there are plenty of ways to solve problems.
    The example below is only one possibility. It uses a join instead of the “or … like”.
    Instead of the subquery you could use a real table.
    Also there might be inventive ways of using LOCATE, REGEXP/RLIKE…

    select v1.* from mysql.help_keyword v1,
      (select '%TREE%' as sw union select '%TYPE%') v2
    where v1.name like v2.sw;
    
    +-----------------+-------+
    | help_keyword_id | name  |
    +-----------------+-------+
    |              87 | RTREE |
    |             107 | TYPE  |
    |             441 | BTREE |
    |             445 | TYPES |
    +-----------------+-------+
    4 rows in set (0.03 sec)
    


You must be logged in to post a comment.