简体   繁体   中英

Error in SQL SELECT with INNER JOIN

So, basically, I have two tables called "dadoscatalogo" and "palavras_chave", with a common field, "patrimonio" which is the primary key of "dadoscatalogo".

I'm using a servlet to connect to the database with these tables, and passing a query to search for entries based on some search criteria that's defined by the user.

Now, since the user can search for entries based on information present in both tables, I need to do an INNER JOIN , and then use WHERE to search for that info. I'm also using LIKE , because the user may pass just part of the information, and not all of it.

So, to test it all out, I tried passing it a few parameters to work with, and see how it went. After some debugging, I found out that there was some mistake in the query. But I can't seem to be able to point out exactly what it is.

Here's the test query:

SELECT dadoscatalogo.patrimonio
FROM dadoscatalogo 
  INNER JOIN palavras_chave 
    ON dadoscatalogo.patrimonio=palavras_chave.patrimonio 
WHERE dadoscatalogo.patrimonio LIKE '%' 
  AND dadoscatalogo.titulo LIKE '%tons%' 
   OR palavras_chave.palchave LIKE '%programming%';

So, basically, what I'm trying to do with this query is, get all the primary keys from "dadoscatalogo" that are linked to a record with a "titulo" containing "tons", or a "palchave" containing "programming".

PS. Sorry for the names not being in English, hopefully it won't be too much of a distraction.

EDIT: Right now, the tables don't have much:

This is the dadoscatalogo table: http://gyazo.com/fdc848da7496cea4ea2bcb6fbe81cb25

And this is the palavras_chave table: http://gyazo.com/6bb82f844caebe819f380e515b1f504e

When they join, I'm expecting it to have 4 records, and it would get the one with patrimonio=2 in dadoscatalogo (which has "tons" in titulo), and the one with palchave=programming (which would have patrimonio=1)

As per my understanding run below query:

 SELECT dadoscatalogo.patrimonio
FROM dadoscatalogo 
  INNER JOIN palavras_chave 
   ON dadoscatalogo.patrimonio=palavras_chave.patrimonio 
WHERE dadoscatalogo.titulo LIKE '%tons%' 
  OR palavras_chave.palchave LIKE '%programming%';

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