All Questions

Tagged with
Filter by
Sorted by
Tagged with
2773 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']; ...
2180 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?
Lion King's user avatar
  • 33.4k
1652 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 ...
Majid Fouladpour's user avatar
1630 votes
25 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.
homerun's user avatar
  • 20.3k
1485 votes
16 answers
903k 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, ...
Dean Rather's user avatar
1325 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 ...
Vijay Dev's user avatar
  • 27.2k
1165 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 ...
Keshan's user avatar
  • 14.5k
1119 votes
12 answers
719k 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 ...
JCCyC's user avatar
  • 16.4k
1114 votes
20 answers
637k 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 ...
Your Common Sense's user avatar
1090 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 ...
warren's user avatar
  • 33.1k
911 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, ...
Kaptah's user avatar
  • 9,811
880 votes
15 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?
Spencer's user avatar
  • 21.9k
826 votes
9 answers
899k 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 ...
jul's user avatar
  • 36.9k
798 votes
19 answers
896k 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 ...
Fernando Barrocal's user avatar
797 votes
17 answers
843k views

MySQL: Get character-set of database or table or column?

What is the (default) charset for: MySQL database MySQL table MySQL column
Amandasaurus's user avatar
  • 59.5k
788 votes
13 answers
314k 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 ...
Nate's user avatar
  • 27k
786 votes
4 answers
313k 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(...
Richard Knop's user avatar
  • 82.4k
763 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?
Michael's user avatar
  • 14k
746 votes
10 answers
920k 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?
jimgh's user avatar
  • 7,477
678 votes
12 answers
459k views

How to skip certain database tables with mysqldump?

Is there a way to restrict certain tables from the mysqldump command? For example, I'd use the following syntax to dump only table1 and table2: mysqldump -u username -p database table1 table2 > ...
670 votes
16 answers
553k views

Export schema without data

I'm using a MySql database with a Java program, now I want to give the program to somebody else. How to export the MySQL database structure without the data in it, just the structure?
Darth Blue Ray's user avatar
669 votes
19 answers
1.1m views

How do I restore a dump file from mysqldump?

I was given a MySQL database file that I need to restore as a database on my Windows Server 2008 machine. I tried using MySQL Administrator, but I got the following error: The selected file was ...
Zack Peterson's user avatar
582 votes
9 answers
1.1m views

How to declare a variable in MySQL?

How to declare a variable in mysql, so that my second query can use it? I would like to write something like: SET start = 1; SET finish = 10; SELECT * FROM places WHERE place BETWEEN start AND ...
cdub's user avatar
  • 25.2k
554 votes
29 answers
582k views

MySQL select 10 random rows from 600K rows fast

How can I best write a query that selects 10 rows randomly from a total of 600k?
Francisc's user avatar
  • 79k
553 votes
6 answers
451k views

MySQL: @variable vs. variable. What's the difference?

In another question I posted someone told me that there is a difference between: @variable and: variable in MySQL. He also mentioned how MSSQL has batch scope and MySQL has session scope. Can ...
aarona's user avatar
  • 36.7k
552 votes
29 answers
2.0m views

MySQL Error: : 'Access denied for user 'root'@'localhost'

Consider: ./mysqladmin -u root -p** '_redacted_' Output (including typing the password): Enter password: mysqladmin: connect to server at 'localhost' failed error: 'Access denied for user 'root'@'...
Harsh Trivedi's user avatar
534 votes
14 answers
552k views

Restrict results to top N rows per group

The following query: SELECT year, id, rate FROM h WHERE year BETWEEN 2000 AND 2009 ORDER BY id, rate DESC yields: year | id | rate 2006 | p01 | 8.0 2003 | p01 | 7.4 2008 | p01 | 6.8 2001 | ...
Wells's user avatar
  • 10.6k
513 votes
20 answers
606k views

MySQL error: key specification without a key length

I have a table with a primary key that is a varchar(255). Some cases have arisen where 255 characters isn't enough. I tried changing the field to a text, but I get the following error: BLOB/TEXT ...
GSto's user avatar
  • 41.9k
509 votes
18 answers
736k views

Error related to only_full_group_by when executing a query in MySql

I have upgraded my system and have installed MySql 5.7.9 with php for a web application I am working on. I have a query that is dynamically created, and when run in older versions of MySQL it works ...
Dan Bemowski's user avatar
  • 5,365
494 votes
34 answers
1.1m views

How to select the nth row in a SQL database table?

I'm interested in learning some (ideally) database agnostic ways of selecting the nth row from a database table. It would also be interesting to see how this can be achieved using the native ...
Charles Roper's user avatar
486 votes
18 answers
690k views

Rename a column in MySQL

I am trying to rename a column in MySQL community server 5.5.27 using this SQL expression: ALTER TABLE table_name RENAME COLUMN old_col_name TO new_col_name; I also tried ALTER TABLE table_name ...
Michael Okoli's user avatar
477 votes
2 answers
539k views

Delete all Duplicate Rows except for One in MySQL? [duplicate]

How would I delete all duplicate data from a MySQL Table? For example, with the following data: SELECT * FROM names; +----+--------+ | id | name | +----+--------+ | 1 | google | | 2 | yahoo | |...
Highway of Life's user avatar
466 votes
6 answers
924k views

SQL query return data from multiple tables

I would like to know the following: how to get data from multiple tables in my database? what types of methods are there to do this? what are joins and unions and how are they different from one ...
455 votes
6 answers
365k views

How to use GROUP BY to concatenate strings in MySQL?

Basically the question is how to get from this: foo_id foo_name 1 A 1 B 2 C to this: foo_id foo_name 1 A B 2 C
Paweł Hajdan's user avatar
453 votes
12 answers
359k views

You can't specify target table for update in FROM clause

I have a simple mysql table: CREATE TABLE IF NOT EXISTS `pers` ( `persID` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(35) NOT NULL, `gehalt` int(11) NOT NULL, `chefID` int(11) DEFAULT ...
CSchulz's user avatar
  • 11k
449 votes
20 answers
501k views

Multiple Updates in MySQL

I know that you can insert multiple rows at once, is there a way to update multiple rows at once (as in, in one query) in MySQL? Edit: For example I have the following Name id Col1 Col2 Row1 1 ...
Teifion's user avatar
  • 110k
449 votes
17 answers
454k views

How to create a MySQL hierarchical recursive query?

I have a MySQL table which is as follows: id name parent_id 19 category1 0 20 category2 19 21 category3 20 22 category4 21 ... ... ... Now, I want to have a single MySQL query to which I simply ...
Tarun Parswani's user avatar
445 votes
6 answers
565k views

Update a column value, replacing part of a string

I have a table with the following columns in a MySQL database [id, url] And the URLs are like: http://domain1.example/images/img1.jpg I want to update all the URLs to another domain http://domain2....
Addev's user avatar
  • 32.3k
445 votes
28 answers
473k views

Remove duplicate rows in MySQL

I have a table with the following fields: id (Unique) url (Unique) title company site_id Now, I need to remove rows having same title, company and site_id. One way to do it will be using the ...
Chetan's user avatar
  • 4,915
445 votes
24 answers
460k views

Get record counts for all tables in MySQL database

Is there a way to get the count of rows in all tables in a MySQL database without running a SELECT count() on each table?
Mark's user avatar
  • 4,829
445 votes
17 answers
773k views

What's the difference between VARCHAR and CHAR?

What's the difference between VARCHAR and CHAR in MySQL? I am trying to store MD5 hashes.
steven's user avatar
  • 13.4k
442 votes
29 answers
1.1m views

Getting "Lock wait timeout exceeded; try restarting transaction" even though I'm not using a transaction

I'm running the following MySQL UPDATE statement: mysql> update customer set account_import_id = 1; ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction I'm not using a ...
Jason Swett's user avatar
  • 44.4k
438 votes
37 answers
956k views

Error Code: 2013. Lost connection to MySQL server during query

I got the Error Code: 2013. Lost connection to MySQL server during query error when I tried to add an index to a table using MySQL Workbench. I also noticed that it appears whenever I run long query. ...
user836026's user avatar
436 votes
16 answers
560k views

How can I search (case-insensitive) in a column using LIKE wildcard?

I looked around some and didn't find what I was after so here goes. SELECT * FROM trees WHERE trees.`title` LIKE '%elm%' This works fine, but not if the tree is named Elm or ELM etc... How do I ...
David Morrow's user avatar
  • 9,174
426 votes
21 answers
1.2m views

Error 1046 No database selected, how to resolve?

Error SQL query: -- -- Database: `work` -- -- -------------------------------------------------------- -- -- Table structure for table `administrators` -- CREATE TABLE IF NOT EXISTS `administrators` ...
steph's user avatar
  • 4,277
426 votes
11 answers
336k views

Best database field type for a URL

I need to store a url in a MySQL table. What's the best practice for defining a field that will hold a URL with an undetermined length?
Jesse Hattabaugh's user avatar
422 votes
19 answers
671k views

Get table column names in MySQL?

Is there a way to grab the columns name of a table in MySQL using PHP?
An employee's user avatar
  • 6,248
421 votes
12 answers
486k views

Best way to test if a row exists in a MySQL table

I'm trying to find out if a row exists in a table. Using MySQL, is it better to do a query like this: SELECT COUNT(*) AS total FROM table1 WHERE ... and check to see if the total is non-zero or is ...
Bernard Chen's user avatar
  • 6,477
416 votes
8 answers
1.2m views

MySQL query String contains

I've been trying to figure out how I can make a query with MySQL that checks if the value (string $haystack ) in a certain column contains certain data (string $needle), like this: SELECT * FROM `...
arik's user avatar
  • 28.8k
409 votes
12 answers
358k views

How can I make SQL case sensitive string comparison on MySQL?

I have a function that returns five characters with mixed case. If I do a query on this string it will return the value regardless of case. How can I make MySQL string queries case sensitive?
StevenB's user avatar
  • 4,125

1
2 3 4 5
2807