简体   繁体   中英

MySQL Syntax for linking two rows from different tables, where they both have a common field

I've got a MySQL Database containing several tables and I've got to create a pretty complicated query to get the data I need from these tables, and I am having issues crating said query.

The tables are designed as so (the column names have been renamed, these arn't the actual columns),

Table1 id, name, rating, country

Table2 itemid, textbool1, textbool2

The query needs to:-

  • Check that the table2.textbool1 and table2.textbool2 fields match the inputted string (provided externally, through string concatenation)
  • (Bearing in mind that id = itemid)
  • Check that name contains the inputted string
  • Check that rating is above a certain value
  • Check that country matches the inputted string
  • Return the contents of a maximum of 20 table2 rows, sorted by table1.rating (Descending)

So far, I have this:-

SELECT table2.* from table2 INNER JOIN table1 
WHERE table2.textbool1 = 'true' && table2.textbool2 = 'false' 
&& table1.name LIKE 'test' && table1.rating >= '10' && table1.country = 'gb' 
ORDER BY table1.rating DESC LIMIT 20

But this isn't working. Can anybody explain why?

Any help would be appriciated.

Edit

This query works: SELECT table2.* FROM table2 INNER JOIN table 2 ON(table2.item_id = table1.id) WHERE table1.country= 'de' && premiumservers.Rating >= 9 ORDER BY table1.Rating DESC LIMIT 20

But this does not: SELECT table2.* FROM table2 INNER JOIN tabel1 ON(table2.item_id = table1.id) WHERE table1.Name LIKE 'test' && table1.country = 'de' ORDER BY table1.Rating DESC LIMIT 20

You are missing the ON statement in INNER JOIN

Try this..

SELECT table2.* from 
table2 INNER JOIN table1
ON( table2.itemid = table1.id )
WHERE table2.textbool1 = 'true' && table2.textbool2 = 'false' 
&& table1.name LIKE '%test%' && table1.rating >= '10' && table1.country = 'gb' 
ORDER BY table1.rating DESC LIMIT 20

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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