Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

MySQL Complex Search help

Posted on 2011-02-17
15
Medium Priority
?
1,097 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 39

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 41

Accepted Solution

by:
Sharath earned 500 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 41

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 41

Expert Comment

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

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34918242
change LEFT JOIN to INNER JOIN
0
 

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 41

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 143

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 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Blockchain technology enhances society similar to the Internet. Its effects are broad, disruptive, and will boost global productivity.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

580 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