logo

Coding By Instinct

I love it when you don't know how to code something, so, before you cheat and use Google, you try what your instinct tells you ought to work and, low and behold, it does.

This doesn't happen with Notes as much as it does when I'm learning some other "new" technology. To me it's a sign of how good that technology is. If things work in a way that makes sense then it can't be bad.

It happens quite a lot with Flex, which I find very intuitive, but yesterday it happened with MySQL. Here's what happened:

I have a SQL table, called Hitlog, that records every hit on a document -- what time it was opened and by what user. It looks lie this:

ScreenShot008

What I needed to do was find out not only how many times a document had been read but also by how many people.

From the table above we can see that the document with the ID beginning in 857 has been read 7 times. Not by 7 different people though. Only 4 people have read it. Deandre read it 4 times herself. Maybe she's the author and is trying to make it appear more popular!?

What I needed to do was display on the document itself the following text:

This document has been read 7 times, by 4 different people.

I knew this was possible. I just wasn't sure how.

What's The SQL

It's simple to get the total number of reads, using SQL, like so:

SELECT count(*) FROM hitlog WHERE DocID="8574F37... ...04B0B3C"

This would simply return 7.

It's also simple to get a list of the people who've read it:

SELECT DISTINCT Username FROM hitlog WHERE DocID="8574F37... ...04B0B3C"

Which would return four rows -- one for each person.

What I wanted was a count of the number of these rows though. Also, what I really wanted to do was get both numbers returned from a single SQL statement.

What I didn't know you could do and what I tried as an intuitive solution was the following:

SELECT count(*), count(DISTINCT Username) FROM hitlog WHERE DocID="8574F37... ...04B0B3C"

Et voila, it worked and I got that warm fuzzy feeling you get when you work something out yourself.

Comments

    • avatar
    • CJ
    • Tue 4 Aug 2009 03:38 AM

    Haha, I know what you mean.

    One thing that gets me every now and again (maybe it's just me... not sure!) is when I'm trying to do something quite complicated that I sort of understand but can't seemingly get my head around. I start typing and I'm sort of sure what I'm coding will do the job but I don't really know why, and I have to read back what I typed to myself to understand what's going on.

    • avatar
    • Aaron Hardin
    • Tue 4 Aug 2009 01:05 PM

    I agree. I've got a project going for a friend, selling supp's online. Some of the things we're doing I haven't done before b/c I've done mostly intranet designs, and I'm really enjoying the accomplishments of progress in a new sense.

    It feels good doesn't it!?

  1. I _wish_ code worked out like this for me more often. :P

    • avatar
    • Rieven
    • Wed 5 Aug 2009 08:11 PM

    how i wish notes have SQL statements rather than build views or formulas like that when selecting documents hehe.

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment:


About This Page

Written by Jake Howlett on Tue 4 Aug 2009

Share This Page

# ( ) '

Comments

The most recent comments added:

Skip to the comments or add your own.

You can subscribe to an individual RSS feed of comments on this entry.

Let's Get Social


About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »

More Content