D
D
Dmitry Kuzmin2014-07-30 13:30:13
MySQL
Dmitry Kuzmin, 2014-07-30 13:30:13

How to correctly compose a SELECT from multiple tables?

Good afternoon, help or point in the right direction with a request like this:

SELECT `frk_item` . * , `frk_itemcomment` . *, 
CASE 
WHEN authorId=1 THEN 'Коля' 
END as idname, 
CASE 
WHEN projectId=12 THEN 'TEST' 
END AS grname 
FROM frk_item JOIN `frk_itemcomment` 
USING ( `itemId` ) 
WHERE 
frk_item.itemId in 
(SELECT itemId FROM frk_item WHERE `title` LIKE '%Скрипт рекурсивного мониторинга старых файлов в папках cache%' 
OR `description` LIKE '%Скрипт рекурсивного мониторинга старых файлов в папках cache%') 
OR frk_itemcomment.itemId in 
(SELECT itemId FROM frk_itemcomment WHERE `body` LIKE '%Скрипт рекурсивного мониторинга старых файлов в папках cache%') 
ORDER BY postDate DESC

There are two tables.
The first one contains: authorId, projectId, itemId The
second one contains: itemId, body
The problem with this request is that if body is empty, then the returned result is zero values ​​(although this is not correct)
If body is not empty, then everything works fine.
with body both empty and non-empty, values ​​were chosen.
body - type text
itemId - type int

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2014-07-30
@SashaSkot

..... WHERE body LIKE '%TEXT%' OR body IS NULL

P
podvzbzdnul, 2014-07-30
@podvzbzdnul

It is not clear why, but the results are different if you remove the ORDER BY.
Your WHERE is identical to the following:

WHERE 
frk_item.`title` LIKE '%Скрипт рекурсивного мониторинга старых файлов в папках cache%' 
OR frk_item.`description` LIKE '%Скрипт рекурсивного мониторинга старых файлов в папках cache%' 
OR frk_itemcomment.body LIKE '%Скрипт рекурсивного мониторинга старых файлов в папках cache%'

with whom everything is ok.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question