Solved

MySQL Complex Search help

Posted on 2011-02-17
15
1,079 Views
Last Modified: 2012-05-11
Hello everyone,

I need some help regarding a complex query I am trying to run.

I have 2 tables.  One called Document_List, one called File_Detail.

They both have docID, which is the relationship between the 2 tables.

I want to search this:

Give all rows from File_Detail
Where Document_List.Flag = 6
And File_detail.NotesID = 0

This is my attempt, its almost there, but the notesID = 0 is not working:

select p2.file_detail.* from p2.file_detail 
left join p2.document_list on p2.file_detail.docnumber = p2.document_list.docnumber and p2.file_detail.notesid = 0
where p2.document_list.import_flag = 6;

Open in new window


Can anybody point out where I'm going wrong here please?

Thanks very much!

Darren
0
Comment
Question by:dbridle
  • 7
  • 4
  • 3
  • +1
15 Comments
 
LVL 38

Expert Comment

by:Aaron Tomosky
ID: 34917880
select p2.file_detail.* from p2.file_detail
left join p2.document_list on p2.file_detail.docnumber = p2.document_list.docnumber
Where
p2.file_detail.notesid = 0
And p2.document_list.import_flag = 6;
0
 
LVL 40

Accepted Solution

by:
Sharath earned 125 total points
ID: 34917933
>> This is my attempt, its almost there, but the notesID = 0 is not working:
What do you mean by not working. Are you getting empty result set? run this and see the result.
select p2.file_detail.* from p2.file_detail 
left join p2.document_list on p2.file_detail.docnumber = p2.document_list.docnumber and p2.file_detail.notesid = 0
and p2.document_list.import_flag = 6;

Open in new window

0
 

Author Comment

by:dbridle
ID: 34918064
Thanks for the response guys.

Aaron, your suggestion ended up with the same result set.

Sharath, what I mean by not working, is that I get notesID's of 1, 2, 3 or whatever the value is, when I only want notesid = 0.  I also tried your solution, and the import_flag = 6 is not taking effect, so the notesid is working, but its returning the wrong results from document_list.

Thanks for your help so far!
0
 
LVL 40

Expert Comment

by:Sharath
ID: 34918097
Can you run this and see the result?
SELECT p2.file_detail.* 
  FROM p2.file_detail 
       LEFT JOIN p2.document_list 
         ON p2.file_detail.docnumber = p2.document_list.docnumber 
            AND p2.document_list.import_flag = 6 
 WHERE p2.file_detail.notesid = 0;

Open in new window

0
 

Author Comment

by:dbridle
ID: 34918131
Hi Sharath,

Same result, the import_flag = 6 is not being processed.
0
 
LVL 40

Expert Comment

by:Sharath
ID: 34918234
Is it possible to post the result of my last query and the expected result?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34918242
change LEFT JOIN to INNER JOIN
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:dbridle
ID: 34918293
Tried all of the above variants with INNER JOIN, with no success.

Sharath, I can't post the result set as it contains sensitive data.  I'm trying to think of the best way to replicate the issue.  Perhaps if I sent a create statement of the 2 tables? I'm not sure.
0
 
LVL 40

Expert Comment

by:Sharath
ID: 34918305
I asked sample output because I don't understand by "Same result, the import_flag = 6 is not being processed."
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34918385
hmmmm...

select f.* , f.*
 from p2.file_detail f
left join p2.document_list d
  on f.docnumber = d.docnumber 
 and d.import_flag = 6
where f.notesid = 0;

Open in new window

0
 

Author Comment

by:dbridle
ID: 34918540
Angelll,

The following was in teh result set for notesID - "AFFF8329531912DA802575F40048BDB0".

All results had an import flag of 6.
0
 

Author Comment

by:dbridle
ID: 34918778
Sharath--

"I asked sample output because I don't understand by "Same result, the import_flag = 6 is not being processed.""

I am essentially querying 2 result sets.  The outer result set, which is document_list where import_flag = 6 and in the enner result set, which is file_detail where notesid = 0.

On all of the above idea's, either one or the other where clause works, but never both of them.

So when I say import_flag = 6 is not being processed, or "did not work", what I mean is, that the where clause was ignored.

Hope that helps!
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 125 total points
ID: 34918835
mysql does not ignore clauses.

please post "sample" data, meaning just the relevant part of the output. sensitive data cleared/anonymized.
0
 

Author Comment

by:dbridle
ID: 34919296
I de-sensitized the field names, because it was becoming a hastle.

Here is the query:

select p2.neocol_extraction_file_detail.* 
    from p2.neocol_extraction_file_detail 
        inner join p2.neocol_extract_document_list 
            on p2.neocol_extraction_file_detail.f_docnumber = p2.neocol_extract_document_list.f_docnumber
where p2.neocol_extraction_file_detail.notesid = 0
    and p2.neocol_extract_document_list.import_flag = 6;

Open in new window


Here is a sample results set, with the sensitive data removed and replaced by ***

application/pdf,***, AFFF8329531912DA802575F40048BDB0, 2785437, , 1212671
application/pdf, ***,AFFF8329531912DA802575F40048BDB0, 2785445, , 1212672
application/pdf, ***,D048A4D4CAED8C60802575F6003669A7, 2786092, , 1212680
application/pdf, ***,FF56A4995ED72066802575F6003A4375, 2786240, , 1212684
application/pdf, ***,FF56A4995ED72066802575F6003A4375, 2786257, , 1212685
application/pdf, ***,0, 2786434, , 1212691
application/pdf, ***,BD521712BD104949802575F900449A7A, 2786455, , 1212692

Open in new window


Columns:
Mime Type, Path (***), NotesID, DocID,2ndPath (Usually Blank) , Unique File ID

As you can see from the above query, there is only 1 result in that set that should be there (the notesid = 0.

Here is the table containing the import_flag:

2785437, 46, 1, 1, 6, 14451, 0, 
2785445, 46, 1, 1, 6, 14451, 0, 
2786092, 12, 1, 1, 6, 14451, 0, 
2786240, 5, 1, 1, 6, 14451, 0, 
2786257, 5, 1, 1, 6, 14451, 0, 
2786434, 6, 1, 1, 6, 14451, 0, 
2786455, 12, 1, 1, 6, 14451, 0, 

Open in new window


Columns - Docid, DocClasNum, export_flag, conversion_flag, import_flag (6), entryid, encrpyted_flag.

This is a subset of around 4 million rows, not sure if that has any baring!

Hope that helps.

Darren
0
 

Author Comment

by:dbridle
ID: 34919624
Sometimes, you can work too hard and start to miss stupid things, like not surrounding a string with '.  Changed it to notesid = '0' and works fine.

Really sorry to have wasted your time folks, will split the points between you.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now