2013-05-11 167 views
-2

我需要在名为t_npc的表中搜索列(对于特定值i),但失败很多次。Mysql查询搜索

例如(不工作)

SELECT * FROM t_npc WHERE a_item_0 LIKE '%a_item_%' = 123; 
SELECT * FROM t_npc WHERE 'a_item_0' <=> 'a_item_19' = 123; 
SELECT a_index FROM t_npc WHERE a_item_0 LIKE 'a_item_%' = 123; 
SELECT a_index, a_name FROM t_npc WHERE t_npc.a_item_0 or t_npc.a_item_1 or t_npc.a_item_2 or t_npc.a_item_4 = 44; 

和许多其他搜索下面的值的列名,但它永远不会奏效。我已经尝试过使用这些列的通配符,但仍不幸运。

`a_item_0` int(11) NOT NULL DEFAULT '-1', 
`a_item_1` int(11) NOT NULL DEFAULT '-1', 
`a_item_2` int(11) NOT NULL DEFAULT '-1', 
`a_item_3` int(11) NOT NULL DEFAULT '-1', 
`a_item_4` int(11) NOT NULL DEFAULT '-1', 
`a_item_5` int(11) NOT NULL DEFAULT '-1', 
`a_item_6` int(11) NOT NULL DEFAULT '-1', 
`a_item_7` int(11) NOT NULL DEFAULT '-1', 
`a_item_8` int(11) NOT NULL DEFAULT '-1', 
`a_item_9` int(11) NOT NULL DEFAULT '-1', 
`a_item_10` int(11) NOT NULL DEFAULT '-1', 
`a_item_11` int(11) NOT NULL DEFAULT '-1', 
`a_item_12` int(11) NOT NULL DEFAULT '-1', 
`a_item_13` int(11) NOT NULL DEFAULT '-1', 
`a_item_14` int(11) NOT NULL DEFAULT '-1', 
`a_item_15` int(11) NOT NULL DEFAULT '-1', 
`a_item_16` int(11) NOT NULL DEFAULT '-1', 
`a_item_17` int(11) NOT NULL DEFAULT '-1', 
`a_item_18` int(11) NOT NULL DEFAULT '-1', 
`a_item_19` int(11) NOT NULL DEFAULT '-1', 
+0

是的,我同意下面的家伙。一个新的表格设计是为了 – Drew 2013-05-11 05:54:18

回答

0
select value from table where field = 'value' 

select value from table where field like '%value%' 

你正在做的这两个,这是行不通的。提供更多关于你想要完成的细节,但是从你提出的问题来看,这就是你的查询不起作用的原因。

不过你可以这样做:

select value from table where field_a = 'valuea' AND field_b like '%valueb' 
0

很长的路要走:

SELECT a_index, a_name FROM t_npc WHERE 
t_npc.a_item_0 = 44 or t_npc.a_item_1 = 44 or t_npc.a_item_2 = 44 
... 
or t_npc.a_item19 = 44; 

稍微好一点,你可以使用:

select a_index, a_name from t_npc where 44 in 
(a_item_0, a_item_1, a_item_2, a_item3, 
... 
a_item18, a_item19); 

您还应该创建一个t_npc_items表代替在t_npc上有很多字段。

[你可以通过从INFORMATION_SCHEMA查询字段名,然后执行该命令生成SQL,但metasql使得独角兽哭声。]

+0

是否在那里搜索所有的a_item_x与我们将44添加到所有这些? – user2372172 2013-05-11 16:31:54

+0

是的,请参阅编辑。 – 2013-05-11 16:46:21

0

您应该使用不同的表结构。

尝试这样的事情

CREATE TABLE npc (
    Npc_id bigint(20) unsigned NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY (Npc_id) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 


CREATE TABLE item (
    Npc_id bigint(20) unsigned NOT NULL, 
    Item_id bigint(20) unsigned NOT NULL AUTO_INCREMENT, 
    Item_value int(11) NOT NULL, 
    PRIMARY KEY (Item_id), 
    KEY Npc_id (Npc_id), 
    CONSTRAINT Item_ibfk_1 FOREIGN KEY (Npc_id) REFERENCES Npc (Npc_id) ON DELETE CASCADE 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 
+0

感谢您的帮助..sadly没有工作....迈克尔感谢硬效果,但你的项目表有一个foreigh关键错误,当我尝试查询 – user2372172 2013-05-11 16:50:07