{"id":225967,"date":"2022-08-17T13:06:00","date_gmt":"2022-08-17T10:06:00","guid":{"rendered":"https:\/\/wordpress.mediadoma.com\/?p=225967"},"modified":"2022-11-08T01:34:57","modified_gmt":"2022-11-07T22:34:57","slug":"frammenti-di-tabella-mysql-e-correzioni-a-problemi-comuni","status":"publish","type":"post","link":"https:\/\/wordpress.mediadoma.com\/it\/frammenti-di-tabella-mysql-e-correzioni-a-problemi-comuni\/","title":{"rendered":"Frammenti di tabella MySQL e correzioni a problemi comuni"},"content":{"rendered":"\n<p>Frammenti di tabella MySQL?<\/p>\n<p>S\u00ec, ce ne sono parecchi in questo articolo. Ma prima di arrivare a questo, cos&#8217;\u00e8 MySQL? Cominciamo con la definizione MySQL. MySQL \u00e8 un sistema di gestione di database relazionali (RDBMS) open source supportato da Oracle basato su SQL (Structured Query Language).<\/p>\n<p>Le applicazioni che utilizzano il database MySQL includono TYPO3, MODx, Joomla, WordPress, Simple Machines Forum, phpBB, MyBB e Drupal. MySQL viene utilizzato anche in molti siti Web di alto profilo e su larga scala, inclusi Google (sebbene non per le ricerche), Facebook, Twitter, Flickr e YouTube.<\/p>\n<p>E dopo tutta quell&#8217;introduzione, ti rendi conto che MySQL \u00e8 pi\u00f9 grande di quanto pensassi inizialmente. A meno che tu non sia nel settore da un po&#8217;. Se sei un nuovo arrivato, tuttavia, e vuoi saperne di pi\u00f9 su MySQL, il modo pi\u00f9 semplice per farlo \u00e8 con i frammenti di tabella MySQL.<\/p>\n<h2>Frammenti di tabella MySQL<\/h2>\n<p><strong>Abbina una stringa all&#8217;inizio della stringa<\/strong><\/p>\n<p><code>SELECT 'Test' REGEXP '^The'; -- 0 SELECT 'The Test' REGEXP '^The'; -- 1<\/code><\/p>\n<p><strong>Se un nome non \u00e8 preceduto da &#8216;The&#8217;, aggiungilo<\/strong><\/p>\n<p><code>UPDATE [table] SET Name = CONCAT('The ', TRIM(Name)) WHERE Name NOT REGEXP '^The'<\/code><\/p>\n<p><strong>Copia una colonna da una tabella all&#8217;altra<\/strong><\/p>\n<p><code>INSERT INTO [table] (<\/code><\/p>\n<p><code>) SELECT FROM [table]<\/code><\/p>\n<p><strong>Rimuovi tutti gli spazi bianchi<\/strong><\/p>\n<p><code>UPDATE [table] SET<\/code><\/p>\n<p><code>= REPLACE(, ' ', '')<\/code><\/p>\n<p><strong>(questo \u00e8 BASH) elimina tutte le tabelle in un database<\/strong><\/p>\n<p><code>mysqldump -u[USERNAME] -p[PASSWORD] --add-drop-table --no-data [DATABASE] | grep ^DROP | mysql -u[USERNAME] -p[PASSWORD] [DATABASE]<\/code><\/p>\n<p><strong>Passa alle regole di confronto di una tabella e di tutte le sue colonne esistenti<\/strong><\/p>\n<p><code>alter table [table] convert to character set utf8 collate utf8_general_ci;<\/code><\/p>\n<p><strong>MySQL Trova e sostituisci<\/strong><\/p>\n<p><code>UPDATE files SET filepath = REPLACE(filepath,'path\/to\/search','path\/to\/replace');<\/code><\/p>\n<p><strong>Selezione di una riga casuale in MySQL<\/strong><\/p>\n<p><code>SELECT column FROM table ORDER BY RAND() LIMIT 1<\/code><\/p>\n<p><strong>Crea CSV da MySQL<\/strong><\/p>\n<p><code>SELECT a,b,c FROM my_table INTO OUTFILE '\/ca.csv' FIELDS TERMINATED BY ',' LINES TERMINATED BY 'n';<\/code><\/p>\n<p><strong>Trova record duplicati<\/strong><\/p>\n<p><code>SELECT id, COUNT(*) as n FROM my_table GROUP BY id HAVING n &gt;1;<\/code><\/p>\n<p><strong>Correzione della codifica dei caratteri errata nei dati MySQL<\/strong><\/p>\n<p><code>UPDATE table SET column=CONVERT(CONVERT(CONVERT(column USING latin1) USING binary) using utf8);<\/code><\/p>\n<p><strong>Carica di nuovo CSV in MySQL<\/strong><\/p>\n<p><code>LOAD DATA local INFILE 'unique.csv' INTO TABLE tablename FIELDS TERMINATED BY ',' LINES TERMINATED BY 'n' (col1, col2, col3)<\/code><\/p>\n<p><strong>Drupal 6.x Trova il codice PHP nel database<\/strong><\/p>\n<p><code>select nid, vid from node_revisions where format in (select format from filters where module = 'php' and delta = 0); select cid, nid from comments where format in (select format from filters where module = 'php' and delta = 0); select bid, info from boxes where format in (select format from filters where module = 'php' and delta = 0);<\/code><\/p>\n<p><strong>Genera una stringa casuale in MySQL<\/strong><\/p>\n<p><code>SELECT SUBSTRING(MD5(RAND()) FROM 1 FOR 6) AS randomstring<\/code><\/p>\n<p><strong>Calcola l&#8217;et\u00e0 dalla data di nascita<\/strong><\/p>\n<p><code>SELECT FLOOR(DATEDIFF(CURRENT_DATE(), dob) \/ 365);<\/code><\/p>\n<p><strong>Trova e sostituisci<\/strong><\/p>\n<p><code>UPDATE table_name SET column_name = REPLACE(column_name, 'http:\/\/oldcontent.com', 'http:\/\/newcontent.com');<\/code><\/p>\n<p><strong>Scegli tra due date<\/strong><\/p>\n<p><code>SELECT * FROM files LEFT JOIN (dates) ON files.uploaded_id = dates.id WHERE date BETWEEN '2009-01-01' AND '2009-12-31'<\/code><\/p>\n<p><strong>Backup del database MySQL su file SQL con mysqldump<\/strong><\/p>\n<p><code>mysqldump --user=username --password=password --opt --add-drop-database --databases databasename &gt; ~\/path\/to\/file.sql<\/code><\/p>\n<p><strong>come calcolare gli ultimi 7 giorni<\/strong><\/p>\n<p><code>WHERE mydatefld &gt;= DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY)<\/code><\/p>\n<p><strong>discarica di MySQL<\/strong><\/p>\n<p><code>mysqldump -u [username] -p [password] [databasename] &gt; [backupfile.sql]<\/code><\/p>\n<p><strong>Sostituire MySQL<\/strong><\/p>\n<p>`# Sostituisci la stringa in tutti i record di una tabella<br \/>\nUPDATE nome_tabella SET nome_colonna = REPLACE( nome_colonna, &#8216;\/dev&#8217;, &quot; );<\/p>\n<p>SELECT REPLACE( column_name, &#8216;\/dev&#8217;, &#8220;) FROM table_name;`<\/p>\n<p><strong>Converti fuso orario in locale<\/strong><\/p>\n<p><code>SELECT CONVERT_TZ(mydatefield,'US\/Pacific','CET') FROM mytable;<\/code><\/p>\n<p><strong>Cerca e sostituisci su pi\u00f9 righe in SQL<\/strong><\/p>\n<p><code>UPDATE wp_posts SET post_content = REPLACE(post_content, 'staging.server.com', 'www.productionserver.com');<\/code><\/p>\n<p><strong>Come salvare i risultati da MySQL in un file esterno<\/strong><\/p>\n<p><code>echo \"SELECT columns FROM table_name WHERE whatever='something'\" | \/path\/to\/mysql -uUSERNAME -pPASSWORD DATABASENAME &gt; \/tmp\/outfile.txt;<\/code><\/p>\n<p><strong>Inserisci o aggiorna se esiste gi\u00e0 una chiave univoca<\/strong><\/p>\n<p><code>INSERT INTO `tableName` VALUES (?,?,?,'-1',?,'0') ON DUPLICATE KEY UPDATE `id` = ?, `foo` = ?, `bar` = ?<\/code><\/p>\n<p><strong>mySQL: comandi di backup del database<\/strong><\/p>\n<p><code>-- dump mySQL database to file: VAR=$(date +\u00c3\u00a2\u00ef\u00bf\u00bd\u00ef\u00bf\u00bd%y-%m-%d\u00c3\u00a2\u00ef\u00bf\u00bd\u00ef\u00bf\u00bd); mysqldump -v -uUSERNAME -pPASSWORD -hlocalhost DATABASENAME &gt; \/BACKUPDIR\/$VAR-DATABASENAME.sql ;<\/code><\/p>\n<p><code>-- dump mySQL database to file and then move it: VAR=$(date +\u00c3\u00a2\u00ef\u00bf\u00bd\u00ef\u00bf\u00bd%y-%m-%d\u00c3\u00a2\u00ef\u00bf\u00bd\u00ef\u00bf\u00bd); mysqldump -v -uUSERNAME -pPASSWORD -hlocalhost DATABASENAME &gt; \/BACKUPDIR\/$VAR-DATABASENAME.sql ; mv \/BACKUPDIR\/DATABASENAME.sql \/OTHERBACKUPDIR\/$VAR-DATABASENAME.sql ;<\/code><\/p>\n<p><strong>Importazione MySql<\/strong><\/p>\n<p><code>mysql -u [username] -p [dbname] &lt; [filename].sql<\/code><\/p>\n<p><strong>trovare record duplicati<\/strong><\/p>\n<p><code>select address, count(address) as cnt from mailing_list group by address having cnt &gt; 1 order by cnt;<\/code><\/p>\n<p><strong>MySQL Trova e sostituisci<\/strong><\/p>\n<p><code>update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, 'find this string', 'replace found string with this string');<\/code><\/p>\n<p><strong>Cambia il percorso della directory del file Drupal<\/strong><\/p>\n<p><code>UPDATE FILES SET filepath = REPLACE(filepath, 'sites\/default\/files\/old\/','sites\/default\/files\/new\/');<\/code><\/p>\n<p><strong>Trova tutte le tabelle nel database MySQL contenenti nomi di colonne specifici<\/strong><\/p>\n<p><code>SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME IN ('columnA','ColumnB') AND TABLE_SCHEMA='YourDatabase';<\/code><\/p>\n<p><strong>Trova i valori che si verificano esattamente una volta nella tabella<\/strong><\/p>\n<p><code>SELECT email FROM users GROUP BY email HAVING (COUNT(email) = 1 )<\/code><\/p>\n<p><strong>Script MySql per trovare indirizzi email non validi<\/strong><\/p>\n<p><code>SELECT * FROM people WHERE email NOT LIKE '%_@__%.__%'<\/code><\/p>\n<p><strong>MySQL Calcolo dell&#8217;et\u00e0<\/strong><\/p>\n<p><code>DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(DOB, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') &lt; DATE_FORMAT(DOB, '00-%m-%d')) &gt;= 65<\/code><\/p>\n<p><strong>Left Join per collegare tre o pi\u00f9 tabelle<\/strong><\/p>\n<p><code>select * from (bdg left join res on bdg.bid = res.bid) left join dom on res.rid = dom.rid where dom.rid is NULL;<\/code><\/p>\n<p><strong>MySQL REGEX corrisponde all&#8217;elenco delimitato da virgole<\/strong><\/p>\n<p><code>WHERE id REGEXP '(,|^){$this-&gt;id}(,|$)'<\/code><\/p>\n<p><strong>Ottieni record casuali in base al peso<\/strong><\/p>\n<p><code>SELECT * FROM Table ORDER BY Rand() * (1 \/ Weight<\/code><\/p>\n<p>Ci auguriamo che questi frammenti di tabella MySQL siano ci\u00f2 che stavi cercando. Aggiorneremo questo articolo con altri frammenti di MySQL che incontreremo in futuro, quindi assicurati di aggiungerlo ai segnalibri.<\/p>\n<p>Se ti \u00e8 piaciuto leggere questo articolo sui frammenti di tabella MySQL, dovresti leggere anche questi:<\/p>\n<ul>\n<li><a href=\"https:\/\/wordpress.mediadoma.com\/it\/creazione-di-un-sistema-crud-in-wordpress-plugin-tabelle-wpdatatables\/\" title=\"Creazione di un sistema CRUD in WordPress (2018)\" >Creazione di un sistema CRUD in WordPress (2018)<\/a><\/li>\n<li><a href=\"https:\/\/wordpress.mediadoma.com\/it\/12-suggerimenti-per-creare-un-tema-o-un-plug-in-wordpress-di-successo\/\" title=\"12 suggerimenti per creare un plugin o un tema WordPress Premium di successo\" >12 suggerimenti per creare un plugin o un tema WordPress Premium di successo<\/a><\/li>\n<li><a href=\"https:\/\/wpdatatables.com\/how-to-host-a-website\/\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">Come ospitare un sito Web: tutti i passaggi spiegati<\/a><\/li>\n<li><a href=\"https:\/\/websensepro.com\/blog\/improve-database-performance-by-cleaning-wp_options-table\/\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">Ottimizzazione della tabella WP_OPTIONS di WordPress<\/a><\/li>\n<\/ul>\n<p><div id=\"PostUnique_PostSource\" style=\"padding-top: 50px\">Fonte di registrazione:  <a target=\"_blank\" rel=\"noopener nofollow\" href=\"\/\/wpdatatables.com\" class=\"external external_icon\">wpdatatables.com<\/a><\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Frammenti di tabella MySQL? S\u00ec, ce ne sono parecchi in questo articolo. Ma prima di arrivare a questo, cos&#8217;\u00e8 MySQL? Cominciamo con la definizione MySQL.<\/p>\n","protected":false},"author":1,"featured_media":206960,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_wp_rev_ctl_limit":""},"categories":[835,844,865],"tags":[1168],"class_list":["post-225967","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-guida-per-principianti","category-tutorial","category-wordpress-6","tag-affiai-it"],"_links":{"self":[{"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/posts\/225967","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/comments?post=225967"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/posts\/225967\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/media\/206960"}],"wp:attachment":[{"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/media?parent=225967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/categories?post=225967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/it\/wp-json\/wp\/v2\/tags?post=225967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}