List number of columns in mysql [duplicate] - php

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';

Related

Get last generated id in MySql [duplicate]

This question already has answers here:
Get current AUTO_INCREMENT value for any table
(9 answers)
Closed 6 years ago.
I have an auto-increment column 'id' . Now I want to echo the last generated id.
I have tried these queries -
SELECT MAX(id) FROM table_name
SELECT id from table_name ORDER BY id DESC LIMIT 1
SELECT LAST_INSERT_ID();
Above two queries returning the Max Id which is actually the last id generated.
But the problem is suppose if I delete a row where id is 10, then again I insert a row. Then the id would be 11 in the last row, not 10. But these 2 queries are returning the value 9. But I want the value 10.
LAST_INSERT_ID() should be used after an insert query which I dont need to do.
How can I get the id value of 10 ?
SELECT `AUTO_INCREMENT`
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'DatabaseName'
AND TABLE_NAME = 'TableName';
This was taken from
Get current AUTO_INCREMENT value for any table

Delete all the tables exept one? [duplicate]

This question already has answers here:
Drop All Tables Except One in MySql
(6 answers)
Closed 6 years ago.
I was wonder if there is any way to delete all the table of my bdd exept the one named "keep" in php. Something like this:
$sql = 'DROP TABLE * EXEPT 'keep' ';
Also is there any command in php to create a new bdd (not a table) or do I have to do it in phpmyadmin ?
Thanks in advance !
You can create a "drop table" sql file by accessing the information_schema.tables table and selecting from it for the appropriate schema.
select concat('drop table ','table_name',';')
from information_schema.tables
where table_schema = '[my_schema_name]'
and table_name != '[the_table_I_don't_want_to_drop]'
INTO OUTFILE '[FILESPEC]';
If you would like to list a group of tables not to drop, simply change the and clause to "and table_name not in ('table1','table2',...'tableN')".
You could do the same thing in PHP. You would simply loop through the results and execute the resultant string.

Is there have the best way to count all rows in table mysql? [duplicate]

This question already has answers here:
MySQL: Fastest way to count number of rows
(14 answers)
Closed 8 years ago.
I want to count all of rows in table that match to my condition as fast as mysql can do.
So, i have four SQL and want you to explain all of them, how is it different for each SQL?
and which is fastest or best for query times and server performance? or it has another way that can better than these. Thank you.
select count(*) as total from table where my_condition
select count(1) as total from table where my_condition
select count(primary_key) as total from table where my_condition
or
select * from table where my_condition
//and then use mysql_num_rows()
First of all don't even think about doing the last one! It literally means select all the data I have in the database, return it to the client and the client will count them!
Second, you need to understand that if you're counting by column name, count will not return null values, for example: select count(column1) from table_name; if there is a row where column1 is null, it will not count it, so be careful.
select count(*), select count(1), select count(primary_key) are all equal and you'll see no difference whatsoever.

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

Retrieve only numbers using sql query [duplicate]

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?

Categories