简体   繁体   English

如何在一个查询中结合2选择?

[英]How to combine 2 select in one query?

I have sql query which selects some stuff from first table using params from second, and then i need to get some columns from second table in addition to first Code looks like that 我有一个SQL查询,它使用第二个参数从第一张表中选择一些东西,然后我需要从第二张表中获取一些列,除了第一张代码看起来像这样

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 ?

But it's throws an error 但这会引发错误

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT entry, name_loc8, description_loc8 FROM locales_item WHERE name_loc8 LIKE' at line 1 检查与您的MySQL服务器版本相对应的手册,以在第1行的“ SELECT条目,name_loc8,description_loc8 FROM locales_item WHERE name_loc8 LIKE”附近使用正确的语法

How to solve this? 如何解决呢?

you should use join to connect the two tables: 您应该使用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; 

if entry is a unique key then you can join the two tables together like so 如果entry是唯一键,则可以像这样将两个表连接在一起

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

this removes the need for your dependent subquery with the IN() since joining the table already filters like the where would 这消除了使用IN()进行依赖子查询的需要,因为加入该表已经像where一样过滤了

If you want to join the results of two queries you can use UNION , however I think what you are after here is joining the matching records in two tables into a single query which you can do with JOIN . 如果要联接两个查询的结果,可以使用UNION ,但是我想这里的工作是将两个表中的匹配记录联接到单个查询中,您可以使用JOIN进行处理

With your example, I think what you're looking for is a query like this: 以您的示例为例,我认为您正在寻找的是这样的查询:

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; 

Try This 尝试这个

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; 

and also you don't need the "AS" after every column if you're calling it the same name. 而且,如果您将其命名为相同的名称,则无需在每列之后都使用“ AS”。

//use join query //使用联接查询

SELECT a.field1,a.field2,b.field3,b.field4 FROM table1 a,table2 b WHERE a.field1=b.field3 ORDER BY a.field4 从table1 a,table2 b中选择a.field1,a.field2,b.field3,b.field4其中a.field1 = b.field3排序a.field4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM