662,086
questions
3192
votes
40
answers
1.1m
views
Should I use the datetime or timestamp data type in MySQL?
Would you recommend using a datetime or a timestamp field, and why (using MySQL)?
I'm working with PHP on the server side.
2797
votes
57
answers
5.2m
views
How to import an SQL file using the command line in MySQL?
I have a .sql file with an export from phpMyAdmin. I want to import it into a different server using the command line.
I have a Windows Server 2008 R2 installation. I placed the .sql file on the C ...
2769
votes
27
answers
2.2m
views
How can I prevent SQL injection in PHP?
If user input is inserted without modification into an SQL query, then the application becomes vulnerable to SQL injection, like in the following example:
$unsafe_variable = $_POST['user_input'];
...
2727
votes
14
answers
260k
views
Why shouldn't I use mysql_* functions in PHP?
What are the technical reasons for why one shouldn't use mysql_* functions? (e.g. mysql_query(), mysql_connect() or mysql_real_escape_string())?
Why should I use something else even if they work on ...
2192
votes
2
answers
2.2m
views
What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN? [duplicate]
What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN
in MySQL?
1999
votes
12
answers
2.9m
views
Installing specific package version with pip
I am trying to install version 1.2.2 of MySQL_python, using a fresh virtualenv created with the --no-site-packages option. The current version shown in PyPi is 1.2.3. Is there a way to install the ...
1670
votes
27
answers
2.1m
views
SQL select only rows with max value on a column [duplicate]
I have this table for documents (simplified version here):
id
rev
content
1
1
...
2
1
...
1
2
...
1
3
...
How do I select one row per id and only the greatest rev?
With the above data, the ...
1651
votes
27
answers
1.9m
views
How to reset AUTO_INCREMENT in MySQL
How can I reset the AUTO_INCREMENT of a field?
I want it to start counting from 1 again.
1493
votes
16
answers
915k
views
Can I concatenate multiple MySQL rows into one field?
Using MySQL, I can do something like:
SELECT hobbies FROM peoples_hobbies WHERE person_id = 5;
My Output:
shopping
fishing
coding
but instead I just want 1 row, 1 col:
Expected Output:
shopping, ...
1441
votes
41
answers
1.7m
views
How can I output MySQL query results in CSV format?
Is there an easy way to run a MySQL query from the Linux command line and output the results in CSV format?
Here's what I'm doing now:
mysql -u uid -ppwd -D dbname << EOQ | sed -e 's/ /,/...
1419
votes
17
answers
1.5m
views
How can I get a list of user accounts using the command line in MySQL?
I'm using the MySQL command-line utility and can navigate through a database. Now I need to see a list of user accounts. How can I do this?
I'm using MySQL version 5.4.1.
1405
votes
13
answers
1.1m
views
Which MySQL data type to use for storing boolean values
Since MySQL doesn't seem to have any 'boolean' data type, which data type do you 'abuse' for storing true/false information in MySQL?
Especially in the context of writing and reading from/to a PHP ...
1359
votes
13
answers
213k
views
UTF-8 all the way through
I'm setting up a new server and want to support UTF-8 fully in my web application. I have tried this in the past on existing servers and always seem to end up having to fall back to ISO-8859-1.
Where ...
1341
votes
9
answers
646k
views
What's the difference between utf8_general_ci and utf8_unicode_ci?
Between utf8_general_ci and utf8_unicode_ci, are there any differences in terms of performance?
1341
votes
34
answers
1.2m
views
Retrieving the last record in each group - MySQL
There is a table messages that contains data as shown below:
Id Name Other_Columns
-------------------------
1 A A_data_1
2 A A_data_2
3 A A_data_3
4 B ...
1285
votes
27
answers
1.5m
views
How do I connect to a MySQL Database in Python?
How do I connect to a MySQL database using a python program?
1220
votes
23
answers
3.1m
views
MySQL error code: 1175 during UPDATE in MySQL Workbench
I want to update the column visited to give it the value 1. I tried this command in the SQL editor inside MySQL workbench:
UPDATE tablename SET columnname=1;
But I get an error that says:
You are ...
1176
votes
13
answers
1.3m
views
Insert into a MySQL table or update if exists
I want to add a row to a database table, but if a row exists with the same unique key I want to update the row.
For example:
INSERT INTO table_name (ID, NAME, AGE) VALUES(1, "A", 19);
Let’s ...
1174
votes
21
answers
946k
views
How to get the sizes of the tables of a MySQL database?
I can run this query to get the sizes of all tables in a MySQL database:
show table status from myDatabaseName;
I would like some help in understanding the results. I am looking for tables with the ...
1161
votes
46
answers
1.7m
views
How do I rename a MySQL database (change schema name)?
How do I quickly rename a MySQL database (change its schema name)?
Usually I just dump a database and re-import it with a new name. This is not an option for very big databases. Apparently RENAME {...
1130
votes
20
answers
653k
views
Join vs. sub-query
I am an old-school MySQL user and have always preferred JOIN over sub-query. But nowadays everyone uses sub-query, and I hate it; I don't know why.
I lack the theoretical knowledge to judge for ...
1124
votes
12
answers
724k
views
INNER JOIN ON vs WHERE clause
For simplicity, assume all relevant fields are NOT NULL.
You can do:
SELECT
table1.this, table2.that, table2.somethingelse
FROM
table1, table2
WHERE
table1.foreignkey = table2.primarykey
...
1098
votes
12
answers
1.1m
views
How can I do 'insert if not exists' in MySQL?
I started by googling and found the article How to write INSERT if NOT EXISTS queries in standard SQL which talks about mutex tables.
I have a table with ~14 million records. If I want to add more ...
1052
votes
11
answers
739k
views
How can I find all the tables in MySQL with specific column names in them?
I have 2-3 different column names that I want to look up in the entire database and list out all tables which have those columns. Is there any easy script?
1051
votes
15
answers
712k
views
How do I specify unique constraint for multiple columns in MySQL?
I have a table:
table votes (
id,
user,
email,
address,
primary key(id),
);
How can I make the columns user, email, address unique - i.e., ensure that there isn't any pair of rows ...
1047
votes
27
answers
1.7m
views
How do you set a default value for a MySQL Datetime column?
How do you set a default value for a MySQL Datetime column?
In SQL Server it's getdate(). What is the equivalant for MySQL? I'm using MySQL 5.x if that is a factor.
1005
votes
31
answers
777k
views
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
I am trying to select data from a MySQL table, but I get one of the following error messages:
mysql_fetch_array() expects parameter 1 to be resource, boolean given
This is my code:
$username = $...
963
votes
12
answers
1.1m
views
"INSERT IGNORE" vs "INSERT ... ON DUPLICATE KEY UPDATE"
While executing an INSERT statement with many rows, I want to skip duplicate entries that would otherwise cause failure. After some research, my options appear to be the use of either:
ON DUPLICATE ...
955
votes
4
answers
999k
views
TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT maximum storage sizes
Per the MySQL docs, there are four TEXT types:
TINYTEXT
TEXT
MEDIUMTEXT
LONGTEXT
What is the maximum length that I can store in a column of each data type assuming the character encoding is UTF-8?
953
votes
27
answers
842k
views
Finding duplicate values in MySQL
I have a table with a varchar column, and I would like to find all the records that have duplicate values in this column. What is the best query I can use to find the duplicates?
915
votes
9
answers
535k
views
MySQL: Large VARCHAR vs. TEXT?
I've got a messages table in MySQL which records messages between users. Apart from the typical ids and message types (all integer types) I need to save the actual message text as either VARCHAR or ...
914
votes
22
answers
1.9m
views
How can I SELECT rows with MAX(Column value), PARTITION by another column in MYSQL?
I have a table of player performance:
CREATE TABLE TopTen (
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
home INT UNSIGNED NOT NULL,
`datetime`DATETIME NOT NULL,
player VARCHAR(6) NOT NULL,
...
892
votes
25
answers
306k
views
MyISAM versus InnoDB [closed]
I'm working on a projects which involves a lot of database writes, I'd say (70% inserts and 30% reads). This ratio would also include updates which I consider to be one read and one write. The reads ...
892
votes
14
answers
1.1m
views
How can I do a FULL OUTER JOIN in MySQL?
I want to do a full outer join in MySQL. Is this possible? Is a full outer join supported by MySQL?
886
votes
38
answers
1.8m
views
Disable ONLY_FULL_GROUP_BY
I accidentally enabled ONLY_FULL_GROUP_BY mode like this:
SET sql_mode = 'ONLY_FULL_GROUP_BY';
How do I disable it?
886
votes
22
answers
2.6m
views
Host 'xxx.xx.xxx.xxx' is not allowed to connect to this MySQL server
This should be dead simple, but I cannot get it to work for the life of me.
I'm just trying to connect remotely to my MySQL server.
Connecting as:
mysql -u root -h localhost -p
works fine, but ...
873
votes
16
answers
874k
views
How to truncate a foreign key constrained table?
Why doesn't a TRUNCATE on mygroup work?
Even though I have ON DELETE CASCADE SET I get:
ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constraint (mytest.instance, ...
859
votes
12
answers
641k
views
Duplicating a MySQL table, indices, and data
How do I copy or clone or duplicate the data, structure,
and indices of a MySQL table to a new one?
This is what I've found so far.
This will copy the data and the structure,
but not the indices:
...
846
votes
18
answers
778k
views
MySQL Error 1093 - Can't specify target table for update in FROM clause
I have a table story_category in my database with corrupt entries. The next query returns the corrupt entries:
SELECT *
FROM story_category
WHERE category_id NOT IN (
SELECT DISTINCT category....
843
votes
9
answers
942k
views
How can I temporarily disable a foreign key constraint in MySQL?
Is it possible to temporarily disable constraints in MySQL?
I have two Django models, each with a foreign key to the other one. Deleting instances of a model returns an error because of the foreign ...
819
votes
12
answers
871k
views
How to get size of mysql database?
How to get size of a mysql database?
Suppose the target database is called "v3".
805
votes
17
answers
860k
views
MySQL: Get character-set of database or table or column?
What is the (default) charset for:
MySQL database
MySQL table
MySQL column
802
votes
16
answers
685k
views
How do I see all foreign keys to a table or column?
In MySQL, how do I get a list of all foreign key constraints pointing to a particular table? a particular column? This is the same thing as this Oracle question, but for MySQL.
801
votes
19
answers
909k
views
MySQL Query GROUP BY day / month / year
Is it possible to make a simple query to count how many records I have in a determined period of time like a year, month, or day, having a TIMESTAMP field, like:
SELECT COUNT(id)
FROM stats
WHERE ...
797
votes
11
answers
495k
views
What is the best collation to use for MySQL with PHP? [closed]
Is there a collation type which is officially recommended by MySQL, for a general website where you aren't 100% sure of what will be entered? I understand that all the encodings should be the same, ...
797
votes
13
answers
323k
views
When to use single quotes, double quotes, and backticks in MySQL
I am trying to learn the best way to write queries. I also understand the importance of being consistent. Until now, I have randomly used single quotes, double quotes, and backticks without any real ...
791
votes
4
answers
317k
views
SQL injection that gets around mysql_real_escape_string()
Is there an SQL injection possibility even when using mysql_real_escape_string() function?
Consider this sample situation. SQL is constructed in PHP like this:
$login = mysql_real_escape_string(...
786
votes
35
answers
1.1m
views
MySQL 8.0 - Client does not support authentication protocol requested by server; consider upgrading MySQL client
I can't make a simple connection to the server for some reason. I install the newest MySQL Community 8.0 database along with Node.JS with default settings.
This is my node.js code
var mysql = ...
768
votes
8
answers
1.6m
views
'IF' in 'SELECT' statement - choose output value based on column values
SELECT id, amount FROM report
I need amount to be amount if report.type='P' and -amount if report.type='N'. How do I add this to the above query?
753
votes
10
answers
938k
views
Create new user in MySQL and give it full access to one database
I want to create a new user in MySQL and give it full access only to one database, say dbTest that I create with a command like create database dbTest;. What would be the MySQL commands to do that?