Skip to content

Instantly share code, notes, and snippets.

@mauricios
Last active January 30, 2017 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauricios/76bbcd2ffa7ada50bc3bd408a0fbd653 to your computer and use it in GitHub Desktop.
Save mauricios/76bbcd2ffa7ada50bc3bd408a0fbd653 to your computer and use it in GitHub Desktop.
# Create a CSV with query results and save it to a file
SELECT * FROM table INTO OUTFILE '/var/lib/mysql-files/table.csv' FIELDS ENCLOSED BY '"' TERMINATED BY ',' ESCAPED BY '"' LINES TERMINATED BY '\r\n';
# Get selected fields and include a related field from other table
SELECT tableA.filed1, tableA.field2, tableB.filed1, tableB.field2 FROM tableA INNER JOIN tableB ON tableA.id = tableB.id;
# Filter fields with wildcard and order by a cetain field
SELECT * FROM table WHERE field1 LIKE '%@example.com' ORDER BY field2
# Find unique duplicated records of a certain field
SELECT * FROM table GROUP BY field HAVING COUNT(*) >= 2;
# Find duplicated records of a certain field
SELECT field1, field2, table.field3 FROM table INNER JOIN (SELECT field3 FROM table GROUP BY field3 HAVING COUNT(*) > 1) dup ON table.field3 = dup.field3;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment