Solved

MySQL DateTime Query Problem

Posted on 2010-09-03
27
392 Views
Last Modified: 2012-05-10
If I do a query in the MySQL workbench, I get 8 records (which is correct), but when I do the same query in my program, I only get the 1st 2 records. The only thing I'm really querying against is a tblARInvoice.Invoice_Date datetime field.

txtYY.text contains the last 2 digits of the year. ex 10 for 2010
txtMM.text contains 2 digits for the month. ex 09
txtDD.text contains 2 digits for the day. ex 03

The majority of the select statement is below.

What am I doing wrong to get different results?
SELECT 

Clients.FNAME, tblARInvoice.SEQ, tblARInvoice.Invoice_Date AS 'INVOICE_DATE', tblARInvoice.YEAR, tblARInvoice.strStatus AS 'STATUS', tblARReceipts.DATE as 'RECEIPT DATE' 

FROM tblARInvoice INNER JOIN Clients ON tblARInvoice.strClientSIN = Clients.SIN LEFT OUTER JOIN tblARReceipts ON tblARInvoice.strINVOICE_NUMBER = tblARReceipts.strInvoiceNumber 

WHERE (tblARInvoice.Invoice_Date <= '20" & txtYY.Text & "-" & txtMM.Text & "-" & txtDD.Text & "')  ORDER BY tblARReceipts.DATE, Clients.LNAME, Clients.FNAME

Open in new window

0
Comment
Question by:JohnnyBCJ
  • 14
  • 10
  • 2
  • +1
27 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33595550
can you show the code that runs this query?
also, can you output the SQL string value before it's executed?
0
 

Author Comment

by:JohnnyBCJ
ID: 33595590
I sure can angellll

The string value is :

SELECT
Clients.FNAME, tblARInvoice.SEQ, tblARInvoice.Invoice_Date AS 'INVOICE_DATE', tblARInvoice.YEAR, tblARInvoice.strStatus AS 'STATUS', tblARReceipts.DATE as 'RECEIPT DATE'  FROM tblARInvoice INNER JOIN Clients ON tblARInvoice.strClientSIN = Clients.SIN LEFT OUTER JOIN tblARReceipts ON tblARInvoice.strINVOICE_NUMBER = tblARReceipts.strInvoiceNumber
WHERE (tblARInvoice.Invoice_Date <= '2010-09-03')  
ORDER BY tblARReceipts.DATE, Clients.LNAME, Clients.FNAME


The code that runs the query is below:

myConnection = New MySqlConnection(frmLogin.strConnection)

myConnection.Open()

Dim daReportARInvoice As New MySqlDataAdapter("SELECT Clients.FNAME, tblARInvoice.SEQ, tblARInvoice.Invoice_Date AS 'INVOICE_DATE', tblARInvoice.YEAR, tblARInvoice.strStatus AS 'STATUS', tblARReceipts.DATE as 'RECEIPT DATE' FROM tblARInvoice INNER JOIN Clients ON tblARInvoice.strClientSIN = Clients.SIN LEFT OUTER JOIN tblARReceipts ON tblARInvoice.strINVOICE_NUMBER = tblARReceipts.strInvoiceNumber WHERE (tblARInvoice.Invoice_Date <= '20" & txtYY.Text & "-" & txtMM.Text & "-" & txtDD.Text & "')  ORDER BY tblARReceipts.DATE, Clients.LNAME, Clients.FNAME", myConnection)

daReportARInvoice.Fill(dsReportARInvoice)

myConnection.Close()

Open in new window

0
 
LVL 22

Expert Comment

by:Om Prakash
ID: 33595591
Convert string to date
str_to_date('10-10-2006', "%d-%m-%Y");

SELECT 

	Clients.FNAME, 

	tblARInvoice.SEQ, 

	tblARInvoice.Invoice_Date AS 'INVOICE_DATE', 

	tblARInvoice.YEAR, 

	tblARInvoice.strStatus AS 'STATUS', 

	tblARReceipts.DATE as 'RECEIPT DATE' 

FROM tblARInvoice 

	INNER JOIN Clients ON tblARInvoice.strClientSIN = Clients.SIN 

	LEFT OUTER JOIN tblARReceipts ON tblARInvoice.strINVOICE_NUMBER = tblARReceipts.strInvoiceNumber 

WHERE (tblARInvoice.Invoice_Date <= str_to_date('" & txtDD.Text & "-" & txtMM.Text & "-20" & txtYY.Text  & "', "%d-%m-%Y"))  ORDER BY tblARReceipts.DATE, Clients.LNAME, Clients.FNAME

Open in new window

0
 

Author Comment

by:JohnnyBCJ
ID: 33595639
With your suggestion om_prakash_p, I still only get 2 records.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33595687
are you 200% sure execution the SQL on the same database/schema?
0
 

Expert Comment

by:first_major
ID: 33595699
yea. make sure you are using the correct schema, like schema.tablename
0
 

Author Comment

by:JohnnyBCJ
ID: 33595706
How can I double check?
I have 2 MySQL databases and they contain identical information in both databases.


0
 
LVL 22

Expert Comment

by:Om Prakash
ID: 33595790
please check the connection string which is being used for this query
0
 

Author Comment

by:JohnnyBCJ
ID: 33595794
If I try to do the query with db1.tablename.columnnames it works but if I try to do it with db2.tablename.columnnames, it fails.


0
 

Author Comment

by:JohnnyBCJ
ID: 33595820
The connection string is correct.
0
 

Author Comment

by:JohnnyBCJ
ID: 33596074
Let me rephrase myself:

In WorkBench, if I was to select either of the two identical databases (db1.table or db2.table) I get all 8 rows, which is correct.

If I use db1 in my program, I get 2 rows.

If I use db2 in my program, it doesn't work. I would believe this is because the connection string is pointing db1 and not db2.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33596152
>If I use db2 in my program, it doesn't work.
what error?

apart from that, I see no explanation on this "different behaviour"
0
 

Author Comment

by:JohnnyBCJ
ID: 33596247
Another note:

In my connection string I have respect binary flags=false;

I have this because when I was doing queries I would get System.Byte[] except of the string values.

Would this possibly cause my problem?


0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33596262
is any of the fields in the query being returned of type binary/blob/...
0
 

Author Comment

by:JohnnyBCJ
ID: 33596309
My error is  "Unknown column 'db2.clients.lname' in 'field list'"

There are no columns that are binary or blob.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33596383
>My error is  "Unknown column 'db2.clients.lname' in 'field list'"

your query is written incorrectly, then...
you should use table aliases in your queries, like:

myConnection = New MySqlConnection(frmLogin.strConnection)

myConnection.Open()

Dim daReportARInvoice As New MySqlDataAdapter("SELECT c.FNAME, i.SEQ, i.Invoice_Date AS `INVOICE_DATE`, i.`YEAR`, i.strStatus AS `STATUS`, r.`DATE` as `RECEIPT DATE` FROM tblARInvoice i INNER JOIN Clients c ON i.strClientSIN = c.SIN LEFT OUTER JOIN tblARReceipts r ON i.strINVOICE_NUMBER = r.strInvoiceNumber WHERE (i.Invoice_Date <= '20" & txtYY.Text & "-" & txtMM.Text & "-" & txtDD.Text & "') ORDER BY r.`DATE`, c.LNAME, c.FNAME ", myConnection)

daReportARInvoice.Fill(dsReportARInvoice)

myConnection.Close()

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33596389
so, if you now had to specify a db name in the query:
myConnection = New MySqlConnection(frmLogin.strConnection)

myConnection.Open()

Dim daReportARInvoice As New MySqlDataAdapter("SELECT c.FNAME, i.SEQ, i.Invoice_Date AS `INVOICE_DATE`, i.`YEAR`, i.strStatus AS `STATUS`, r.`DATE` as `RECEIPT DATE` FROM db2.tblARInvoice i INNER JOIN db2.Clients c ON i.strClientSIN = c.SIN LEFT OUTER JOIN db2.tblARReceipts r ON i.strINVOICE_NUMBER = r.strInvoiceNumber WHERE (i.Invoice_Date <= '20" & txtYY.Text & "-" & txtMM.Text & "-" & txtDD.Text & "') ORDER BY r.`DATE`, c.LNAME, c.FNAME ", myConnection)

daReportARInvoice.Fill(dsReportARInvoice)

myConnection.Close()

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33596394
finally, it could be permission problems ... (the error message). ..
but 2 records returned instead of 8 ... can only data issues.
0
 

Author Comment

by:JohnnyBCJ
ID: 33596577
I agree. That is the only thing that makes sense to me is it being data related, but I don't know where it could be.   My lack of experience with MySQL doesn't help any. I'm use to MS SQL.


Do you know of any possible tests I can do that might give us more information on what is causing this?




0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33596662
reduce the queries to see as from when it returns to "normal" ...
or start with a minimum query, and add up conditions/joins to see as from when it starts going wrong ...
0
 

Author Comment

by:JohnnyBCJ
ID: 33596917
I'm not quite sure what I did cause I've tried a number of things to get this to work.

The last thing I attempted to do was select put in db1. in front of all column names, and it worked. I get all 8 results.

I assumed it would automatically select the database that is mentioned in the connection string. On top of that, the two databases are identical so I don't understand on why mentioning the name of the database that is mentioned in the connection string would do anything to change the query results.

Any idea what could have caused this?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33596964
sorry, no, I don't see it ...
0
 

Author Comment

by:JohnnyBCJ
ID: 33597176
My Mistake!! This isn't over yet!

I accidently removed the invoice_date portion of the where statement. If I remove the "where arinvoice.invoice_date ... ' portion of the where statement, I get the correct results.

If I keep that portion in, I get no records (where previously I got 2 records).
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33597215
can you double-check that the table/fields have indeed the same data type?
0
 

Author Comment

by:JohnnyBCJ
ID: 33597293
I just double checked. They do have the same data type.
0
 

Accepted Solution

by:
JohnnyBCJ earned 0 total points
ID: 34972271
I believe the problem with this was with the time stamp. If you don't include the time stamp, MySQL assumes the time to be 00:00:00. If the record had a timestamp of 1:20:04, it wouldn't be included in the record. Maybe I'm wrong but the problem seems to be solved now.
0
 

Author Closing Comment

by:JohnnyBCJ
ID: 35964488
My comment is the end solution. Please comment if you feel like you deserve the points.

Thanks for your help as always!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
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…

759 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

18 Experts available now in Live!

Get 1:1 Help Now