Jump to content

[SOLVED] Multiple select advice


JonasE

Recommended Posts

Hello,

 

I currently have two tables:

 

documents with fields:

id

title

 

vartitles with fields:

id

doc_id

vartitle

 

Each record can have one primary title (record.title) and several vartitles (referenced by record_id)

 

Here's my problem: I want to search these two tables for matches to titles. After this, I want to return all the documents matching the the title (i.e. matching vartitle and title fields) regardless of whether match for the title was found in the documents table, or in the vartitles table. I know that I can use UNION ALL to return for instance documents.id and vartitles.record_id fields, but how do I select all records matching either of those? Do i use a subquery of some sort?

 

Thank you,

 

Jonas

Link to comment
Share on other sites

No union. I'm not sure if you've got a full text index or not. Use match() against() if you do.

 
SELECT d.*
FROM documents AS d
LEFT JOIN vartitles AS v ON d.id = v.doc_id
WHERE d.title LIKE '%search%' OR v.vartitle LIKE '%search%'
GROUP BY d.id

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.