Retrieve only numbers using sql query [duplicate] - php

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to only select numeric data from mysql?
Hi,
I want to select all values that are only numbers from the db, using
SELECT column_name(s)
FROM table_name
WHERE column_name = '0..9'
How do I get only numbers.
Thanks Jean

Try this:
SELECT column_name FROM table_name WHERE column_name REGEXP '^[0-9]+$'

There is another thread similar to this that may help:
How to only select numeric data from mysql?

Related

List number of columns in mysql [duplicate]

This question already has answers here:
MySQL query to get column names?
(22 answers)
How do I count columns of a table
(8 answers)
Closed 5 years ago.
I want to list the number of columns in a MySQL database table with PHP. How can I achieve this?
The INFORMATION_SCHEMA built into every MySQL server can be used to find this information.
Try this query to get a list of each table in your current database along with its number of columns.
SELECT TABLE_NAME, COUNT(*) AS column_count
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA=DATABASE()
GROUP BY TABLE_NAME
If you want just one table's results, try something like this.
SELECT COUNT(*) AS column_count
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mytable'
If you want to count number of columns then use
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'database_name'
AND table_name = 'tbl_name';

PHP mysql multiple row most recent result [duplicate]

This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 5 years ago.
I have a mysql table that look like this
I'm trying to create a mysql request that will give me, for a specific node, the last available value of each type ( and I have the list of type in another table ).
I really have no idea if its even possible or the right approach to do this kind of task,... i'm not an expert in mysql
I would be inclined to use a correlated subquery in a WHERE clause. I think you are asking for:
select d.*
from data d
where d.date = (select max(d2.date)
from data d2
where d2.node = d.node and d2.type = d.type
);

Mysql search like google [duplicate]

This question already has an answer here:
MySQL Full-text search and SOUNDEX
(1 answer)
Closed 9 years ago.
I have a mysql table tbl_users which have name field.
This name field have following data.
1. Krishna
2. Tomas
3. Harry
I want to work it like this. When i search with the keyword Thomas it should match Tomas also. I tried with following query but it didn't work.
SELECT * FROM `tbl_user` WHERE name like '%Thomas%'
I am not sure it is possible in mysql or not. Please suggest me how can i do this. I am using php with mysql.
Thanks
Try soundex, below is the example
select name from (
select 'Thomas' as name
union
select 'Tomas' as name
union
select 'Ramprasad' as name
)tmp
where soundex(name)=soundex('Thomas')

how to use a value from sql result as table name for its sub query? [duplicate]

This question already has an answer here:
How to select from MySQL where Table name is Variable
(1 answer)
Closed 9 years ago.
In my application I want use dynamic table name.
In my application I have two queries.
1) SELECT `table_name` FROM `data_tables` WHERE `table_id`="1"
From this query I get a table name and I am saving it in a variable; for e.g: $tab
I have another query:
2) SELECT * FROM `'.$tab.'`;
I want to know is there anyway to club these queries? Is it a good practice?
My application is currently working fine, but I would like some insight.
DECLARE #Name varchar(max)
SELECT #Name =`table_name` FROM `data_tables` WHERE `table_id`="1"
SELECT * FROM `#Name`

Get number of rows in MySQL [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Fastest way to count exact number of rows in a very large table?
What query do I use to get the number of rows that share a common specification.
Example: The number of rows where idpost = 3
You can use COUNT() documented here.
SELECT COUNT(1) FROM posts WHERE idpost = 3
EDIT: Updated according to dbf's suggestion.. Make sure you distinguish between COUNT(*) and COUNT(1), discussed here.
The query looks like this:
SELECT count(*) FROM some_table WHERE (conditions);
In your example:
SELECT count(*) FROM some_table WHERE idpost=3;
More on counting rows in MySQL: MySQL 5.6 Reference Manual: 3.3.4.8. Counting Rows
EDIT:
If you were wondering, which way of counting all rows is better (count(*) or count(1)), see this: What is better in MYSQL count(*) or count(1)?.
Try
SELECT
COUNT(*) as NumberRows
FROM
your_table_name_here
WHERE
idpost = 3
;
Consider learning SQL:
select count(*) from mytable where idpost=3
the select count(*) function grabs the number of rows that meet a certain criteria - define that in the where statement.
http://dev.mysql.com/doc/refman/5.1/en/counting-rows.html - read here

Categories