2015-11-03 114 views
0

我有选择使用从第二PARAMS从第一个表中的一些东西的SQL查询,然后我需要获得除第一 代码从第二表中的一些列看起来像如何在一个查询中合并2个选择?

SELECT 
    entry AS entry, 
    ItemLevel AS ItemLevel, 
    RequiredLevel AS RequiredLevel, 
    InventoryType AS InventoryType, 
    Quality AS Quality, 
    class AS class, 
    subclass AS subclass 
FROM item_template 
WHERE entry IN (SELECT entry FROM locales_item WHERE name_loc8 LIKE ?) 
ORDER BY ItemLevel DESC; 

SELECT entry, name_loc8, description_loc8 FROM locales_item WHERE name_loc8 LIKE ? 

但它抛出一个错误

您的SQL语法错误;检查对应于你的MySQL服务器版本使用附近的“选择项,name_loc8,description_loc8 FROM locales_item WHERE name_loc8 LIKE”在行1

如何解决这个正确的语法手册?

+1

YOu可能想要查看JOIN – Mihai

+1

您尝试一次运行2个查询。你如何执行它们? –

+0

@Mihai你可以用连接格式吗? –

回答

2

你应该使用join到两个表连接:

SELECT 
    item_template.entry AS entry, 
    ItemLevel AS ItemLevel, 
    RequiredLevel AS RequiredLevel, 
    InventoryType AS InventoryType, 
    Quality AS Quality, 
    class AS class, 
    subclass AS subclass 
FROM item_template inner join locales_item 
on item_template.entry = locales_item.entry 
WHERE name_loc8 LIKE ? 
ORDER BY ItemLevel DESC; 
+0

SQL(1064):您在SQL语法中有错误;检查与您的MySQL服务器版本相对应的手册,以便在''附近使用正确的语法'? ORDER BY ItemLevel DESC'at line 11 –

+0

我从原始查询中复制了'?',你需要在那里放置你的查询条件 –

1

如果条目是一个独特的密钥,那么你可以加入两个表一起,像这样

SELECT it.stuff, li.stuff 
FROM item_template it 
JOIN locales_item li ON li.entry = it.entry 
WHERE li.name_loc8 LIKE "some string" 
ORDER BY it.ItemLevel DESC 

这样就省去了您与IN()相关子查询,因为已经加入表过滤像哪来

0

如果你想加入的两个结果查询您可以使用UNION,但是我认为您在此之后将两个表中的匹配记录合并为一个查询,您可以使用JOIN进行查询。

你的榜样,我想你要找的是这样的查询:

SELECT t.entry   AS entry, 
     t.ItemLevel  AS ItemLevel, 
     t.RequiredLevel AS RequiredLevel, 
     t.InventoryType AS InventoryType, 
     t.Quality  AS Quality, 
     t.class   AS class, 
     t.subclass  AS subclass, 
     l.entry   AS locale_entry, 
     l.name_loc8  AS name_loc8, 
     l.description_loc8 AS description_loc8 
FROM item_template t 
INNER JOIN locales_item l ON l.entry = t.entry 
WHERE l.name_loc8 LIKE ? 
ORDER BY t.ItemLevel DESC; 
-1

//使用连接查询

SELECT a.field1,a.field2,b.field3 ,b.field4 FROM表1一个,表2 b,其中 a.field1 = b.field3 ORDER BY a.field4

0

尝试这个

SELECT 
    t1.entry AS entry, 
    t2.name_loc8, 
    t2.description_loc8 
    t1.ItemLevel AS ItemLevel, 
    t1.RequiredLevel AS RequiredLevel, 
    t1.InventoryType AS InventoryType, 
    t1.Quality AS Quality, 
    t1.class AS class, 
    subclass AS subclass 
FROM item_template t1 
left join locales_item t2 on t1.entry = t2.entry 
WHERE t2.name_loc8 LIKE '%addyourtexthere%'----just change the text between the percent signs to the string your looking for 
ORDER BY ItemLevel DESC; 

,并且如果您将其称为同名,则在每列之后不需要“AS”。