2013-05-11 165 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