Solved

php /mysql count statement...

Posted on 2014-12-05
8
455 Views
Last Modified: 2014-12-06
Hi, I have a column as shown below;

Chair_2014_03_03
;Chair_2014_03_05
Chair_2014_03_10
Footrest_2013_03_03
Footrest_2014_03_03
armchairs_2014_03_10
armchairs_2012_03_03
;armchairs_2013_03_03

I need to have a count of all chairs, footrest, and armchairs using php/mysql. I am not sure if there is a function in mysql that will give me the count with involving some php code. Can someone let me know how to do this?

Thank you,
A
0
Comment
Question by:aej1973
  • 4
  • 3
8 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40483901
If there is nothing else in that column, then COUNT(*) will work.  See here http://dev.mysql.com/doc/refman/5.1/en/counting-rows.html and here http://www.w3schools.com/sql/sql_func_count.asp
0
 

Author Comment

by:aej1973
ID: 40483930
Hello Dave, I am familiar with the count statement, but I need to be able to strip the "," and "-201..." to be able to get the correct count.


A
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 250 total points
ID: 40483955
Use a 'LIKE' statement in the WHERE clause.

WHERE column LIKE '%Chair%' AND column LIKE '%Footrest%' AND column LIKE '%armchairs%' AND 

Open in new window

You don't actually need the last one because 'armchairs' already contains the word 'chair'.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40484050
You may want to consider getting the assistance of a professional DBA to help you redesign this table.  You should be able to make queries like this very simply, but it looks like the data structure permits a collection of semi-useless data to invade the rows.

There are regular expressions that can help in the query, but for my money, a PHP script is the way to go.  Make a SELECT that captured all rows and look at each of them.  With a bit of PHP logic you can separate the fields into array collections of the data you want.  Then PHP count() will be your answer.
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40484079
That should probably be an OR statement.
WHERE column LIKE '%Chair%' OR column LIKE '%Footrest%' OR column LIKE '%armchairs%'

Open in new window

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 40484112
@Dave: I think the "default" installation of MySQL makes the character set case-insensitive.  So your earlier comment about Chair being a subset of armchairs is exactly right.  This WHERE clause would also be expected to find them all:

WHERE column LIKE '%cHAIr%' OR column LIKE '%FooTREst%'

Open in new window

0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40484167
While that is generally true... I wouldn't count on it.  And it is not true for database and table names using MyISAM engine on non-Windows systems because database and table names are also used for file names which are case-sensitive on those systems.  Someone noted recently that phpMyAdmin forces database and table names to be lower case at creation.  I think someone got tired of "answering the phone".
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40484522
You're right -- case-insensitivity is not true for database and table names, just MySQL commands and the data that is housed in the database.  And like all things in computers, you can override the standard settings!  In my experience, unless you program it wrong, MySQL will honor the data, byte-by-byte, and will not make any alterations to case upon INSERT.  SELECT can control whether case matters, depending on initialization settings
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
count download link and run update query 9 54
WordPress TK Title 8 23
PHP preg_replace code convert to Delphi 14 30
Problem sending file attachments 8 22
Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

932 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

14 Experts available now in Live!

Get 1:1 Help Now