Solved

php /mysql count statement...

Posted on 2014-12-05
8
449 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 82

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 82

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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 82

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 82

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 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

16 Experts available now in Live!

Get 1:1 Help Now