Forum Moderators: open

Message Too Old, No Replies

Best Way to Search Many Tables at Once (MySQL)

         

JRNeher

4:32 pm on Mar 18, 2008 (gmt 0)

10+ Year Member



First of all, I want to apologize if this question has been answered, I searched and was unable to find anything.

I have a database that holds company information for each month that we receive a job. All of my tables are named by the month and year (i.e. March_2008). Each new month creates a new table. Each table contains the same columns. I want to be able to build in a search component so that I can go back and find old work based on the infomation stored, but am at a loss as to how to go about it. I was originally thinking about using UNION, but I was unsure how that would work with the growing number of tables each month. Does anyone have a suggestion? Also, is there a better/more effecient way to build the table (maybe put everything into one)? The reason for them being seperated into seperate tables now is that we assign an internal number to each one (i.e. 0803001, then the next month would start back over at 0804001), the last three digits are incremented to the next number. I was unsure how to do acheive the right count if everything was in one table. My PHP code grabs the next number for me automatically by counting the number of rows in the table and adding one. I know this isn't a PHP forum, I am not asking for advice on that, just thought someone might have a better way to build the table(s). Thanks for your help in advance.

This is the layout of a table if it helps at all:
Table Name: March_2008
Columns: assignment_num app_name claim_num insured_name ins_co adj_name claim_status date_assigned date_completed date_inspected supp_assigned supp_completed

JRNeher

jtara

5:43 pm on Mar 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd urge you to put all your data in one table. This is asking for trouble, and you are not going to find a neat solution. You won't find a solution in SQL - you will have to use a programatic solution.

I assume the table holds something like invoices, which have a serial number, and you want the serial number of make some real-world sense.

Assigning the serial number doesn't sound difficult. Simply query the database for the highest serial number. Compare the first 4 digits with the current year and month. If they are equal, increment the serial number by one. If they are not equal, assign the current year and month, with "001" appended.

Depending on the database, you may also be able to create a special "autoincrement" function that overrides the standard "add 1".

JRNeher

5:55 pm on Mar 18, 2008 (gmt 0)

10+ Year Member



Thank you for your reply. I like the idea of using just one table, as it would definately be easier for searching. I just don't know how to go about making the "serial number" function properly. If it wasn't clear before, I have a number which uses the last two digits of the year (08), the two digits of the month (03) and then is incremented by the last three digits. So for example if I have 50 entries for the month of March (so I would have 0803001 - 0803050) and 45 entries for February (0802001 - 0802045) how would I go about seperating out the February numbers to get the next available March number (51)? Is there a way to build a query with wildcards? This is the point at which I get lost. Do you have any advise on this? Again, I really appreciate your help with this jtara!

jtara

6:56 pm on Mar 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



how would I go about seperating out the February numbers to get the next available March number (51)?

The February numbers are irrelevant. All you care about is the maximum value for the number, and the current date.

SELECT MAX(serial_number) FROM mytable;

Now, if the first 4 digits of the result are the current year and month, then simply increment the serial number by 1. Otherwise, you start over for the new month.

Of course, the SELECT about and the INSERT have to be in a transaction, to prevent somebody else from slipping-in between the time that your read the max serial number and when you insert the new one.