2

I got a table structure looking like this:

table_items
+----+-------+
| id | title |
+----+-------+
|  1 | Hello |
+----+-------+

table_values
+----+----------+--------------+---------------------+
| id | items_id |   columnname |        value        |
+----+----------+--------------+---------------------+
|  1 |        1 |  description | This is the desc... |
+----+----------+--------------+---------------------+
|  1 |        1 |        hobby | My hobby is comp... |
+----+----------+--------------+---------------------+

I wan't to search in each of the value fields for the Hello item and all other items on the site. What's the best way of doing that?

4

4 回答 4

0

我找到了解决我的问题的方法,它是使用索引,当然还有左连接表。感谢您的所有回答。

于 2013-06-28T11:26:14.190 回答
0

您需要加入 2 个表,将表items_id中的表table_values与表id中的table_items表(具有标题Hello)链接起来

SELECT table_values.*
FROM table_items, table_values
WHERE table_items.title='Hello'
AND table_items.id=table_values.items_id

我假设您只需要table_value信息(因为我们知道 table_items.title 是 Hello,table_items.id 将与 table_values.items_id 相同)

于 2013-06-27T09:44:30.687 回答
0

你可以使用这个 SQL SELECT 命令:

SELECT table_items.title, table_values.columnname, table_values.value FROM table_items
INNER JOIN table_values ON table_items.id = table_values.items_id
WHERE table_items.title LIKE '%Hello%'
UNION
SELECT table_items.title, table_values.columnname, table_values.value FROM table_items
INNER JOIN table_values ON table_items.id = table_values.items_id
WHERE table_values.columnname LIKE '%Hello%' || table_values.value LIKE '%Hello%';

和 SQLFiddle:http ://sqlfiddle.com/#!2/11539/4

于 2013-06-28T12:42:15.800 回答
-1

使用内部联接

SELECT table_values.*
FROM table_values
INNER JOIN table_items
ON table_items.title LIKE CONCAT(table_values.value, '%')
于 2013-06-27T09:39:28.447 回答