Reputation: 78
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
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; 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
How to solve this?
Upvotes: 0
Views: 90
Reputation: 25842
if entry is a unique key then you can join the two tables together like so
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
Upvotes: 1
Reputation: 44
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.
Upvotes: 0
Reputation: 59
//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
Upvotes: -1
Reputation: 817
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.
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;
Upvotes: 0
Reputation: 12953
you should use join to connect the two tables:
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;
Upvotes: 2