Retrieving the last record in each group – MySQL

MySQL 8.0 now supports windowing functions, like almost all popular SQL implementations. With this standard syntax, we can write greatest-n-per-group queries: WITH ranked_messages AS ( SELECT m.*, ROW_NUMBER() OVER (PARTITION BY name ORDER BY id DESC) AS rn FROM messages AS m ) SELECT * FROM ranked_messages WHERE rn = 1; This and other approaches … Read more

What’s the difference between utf8_general_ci and utf8_unicode_ci?

For those people still arriving at this question in 2020 or later, there are newer options that may be better than both of these. For example, utf8_unicode_520_ci. All these collations are for the UTF-8 character encoding. The differences are in how text is sorted and compared. _unicode_ci and _general_ci are two different sets of rules … Read more

How can I output MySQL query results in CSV format?

From Save MySQL query results into a text or CSV file: SELECT order_id,product_name,qty FROM orders WHERE foo = ‘bar’ INTO OUTFILE ‘/var/lib/mysql-files/orders.csv’ FIELDS TERMINATED BY ‘,’ ENCLOSED BY ‘”‘ LINES TERMINATED BY ‘\n’; Note: That syntax may need to be reordered to SELECT order_id,product_name,qty INTO OUTFILE ‘/var/lib/mysql-files/orders.csv’ FIELDS TERMINATED BY ‘,’ ENCLOSED BY ‘”‘ LINES … Read more

Can I concatenate multiple MySQL rows into one field?

You can use GROUP_CONCAT: SELECT person_id, GROUP_CONCAT(hobbies SEPARATOR ‘, ‘) FROM peoples_hobbies GROUP BY person_id; As Ludwig stated in his comment, you can add the DISTINCT operator to avoid duplicates: SELECT person_id, GROUP_CONCAT(DISTINCT hobbies SEPARATOR ‘, ‘) FROM peoples_hobbies GROUP BY person_id; As Jan stated in their comment, you can also sort the values before … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)