2013-05-11 3 views
-2

특정 값 i에 대해 t_npc라는 테이블에서 열 (아래 참조)을 검색해야하지만 여러 번 실패했습니다.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

답변

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

+0

예, 수정 사항을 참조하십시오. –

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

모든 도움을 주셔서 감사합니다 .. 슬픈 듯이 아무 것도 작동하지 않습니다 .... 하드에 대한 Micheal 감사합니다 effert하지만 항목 테이블은 쿼리를 할 때 foreigh 키 오류가 있습니다 – user2372172