Brain Dump – Database Issues

Brain Dump – Database Issues

0

Here we list some handy SQL tips that help you combine queries, stop fields being truncated and run scheduled backups

Laptop screen showing code
Share This:

[Warning…. Entering a slightly geeky zone]

A couple of few things have cropped up in the past two days that have caused a fair bit of hair pulling and have such simple solutions it’s not true.

I’m sure at some point I will need to remember this information so I hope you don’t mind that I put it here for future reference even if it is a bit off topic for this blog.

blank

If you’re not of a technical nature then feel free to fall asleep now… 😉

Problem 1 – Combining multiple SQL queries with sub-select statements (MYSQL).

W3C Launch RSS/Atom Feed Validator
The World Wide Web Consortium has launched their own RSS/Atom Feed validator. It can be...

I have a users table that I wish to join to a table containing the baskets that they have created and the languages of each item the user has selected.

I could have done this using multiple select statements or some kind of array and loop but this proved to be hugely processor intensive – something that we couldn’t have as it the code’s due to run on a shared server.

So, a colleague and I sat down and came up with a rather splendid query, much better than tonnes of evil joins .

Part of the problem also lay in the fact that we wanted a nice tabular layout for the results which wouldn’t work with all of the queries I tried. It was fine for a single language but a right royal mess for table layouts.

Table Layout

Users Table
--------------------------
|User ID   |  User Name  |
--------------------------
Basket Table
---------------------------------------------------------------
|User ID    | Basket ID |  Language ID  | Catalogue Processed |
---------------------------------------------------------------

SQL Statement

SELECT UserTable.UserID, UserName, LanguageID, CatalogueID, Basketid,
(SELECT Count(LanguageID) FROM (BasketTable) where BasketTable.UserID = t.UserID AND 
CatalogueProcessed=1 AND CatalogueID=1 and LanguageID = 1) AS English,
(SELECT Count(LanguageID) FROM (BasketTable) where BasketTable.UserID = t.UserID AND
CatalogueProcessed=1 AND CatalogueID=1 and LanguageID = 2) AS French,
(SELECT Count(LanguageID) FROM (BasketTable) where BasketTable.UserID = t.UserID AND
CatalogueProcessed=1 AND CatalogueID=1 and LanguageID = 3) AS German,
(SELECT Count(LanguageID) FROM (BasketTable) where BasketTable.UserID = t.UserID AND
CatalogueProcessed=1 AND CatalogueID=1 and LanguageID = 4) AS Italian
FROM (UserTable INNER JOIN BasketTable t on UserTable.CD_ID=t.UserID)WHERE 
CatalogueProcessed=1 AND catalogueID=1 GROUP By UserID

Output

 --------------------------------------------------------------------
| UserID | UserName | BasketID | English | French | German | Italian |
 --------------------------------------------------------------------
|   1    |   Katy   |   1234   |    4    |   0    |   2    |   0     |
 --------------------------------------------------------------------
|   2    |   Pierre |   1235   |    0    |   5    |   0    |   4     |
 --------------------------------------------------------------------

Hooray!

Problem 2 – Memo fields being truncated to 255 characters (MS Access).

This one had me stumped for ages. Originally the script did truncate the field in question but it still did so when I removed the truncation – weird! Anyway, after much hair pulling it turned out to be the “SELECT DISTINCT” clause in the SQL statement. I’m not sure if this is a bug in the code or in Access itself and the way it handles DISTINCT but removing it from the SQL Statement returned the full contents of the memo field.

Weird!

Problem 3 – Scheduled Database Backups failing (MYSQL).

This was another annoying one. All user permission were set correctly and the backup ran when manually activated but wouldn’t run as a scheduled task.

It turns out that the run command generated by MYSQL contains an error. Here’s the command auto-generated by MYSQL:

"C:Program FilesMySQLMySQL Administrator 1.1MySQLAdministrator.exe" 
"-UDC:Documents and SettingsKaty WhittonApplication DataMySQL" 
"-c" "-bpTest Backup" "-btC:" "-bxTest Backup"

You can see on the third line there’s a command called “-c”, changing this to “-C” or removing completely allows the backup to run as a scheduled task successfully.

What Are Web Applications And Why Does Your Firm Need Them?
Web applications are, arguably, an essential evolution from regular web pages. You need to keep on top of the current trends to make sure that your business keeps up with technology in order for you to reach as many potential customers as possible

Annoying!

[End slightly geeky zone]

blank

Sorry for the off topic brain dump but if I didn’t put it somewhere I’d forget what I’d done!

blank
About The Author
Katy is always trying to be more productive one day at a time! Whether it's analogue, digital, motivational or psychological who'll try any system that will help her get things done and get organised. As well as running FlippingHeck.com, she also loves making music and reviewing things.
  • Follow Katy Whitton on:
  • blank
  • blank
  • blank
  • blank
  • blank
  • blank
  • blank
  • blank
Please Note: This post may contain affiliate links. By clicking on these links you will not be charged any extra for purchasing goods and services from our preferred partners however flippingheck.com may receive financial compensation which contributes to the running of the site. For more information please read our Advertising & Affiliate Disclosure Policy

Leave a Reply

Your email address will not be published. Required fields are marked *