{"id":226023,"date":"2022-08-17T13:19:00","date_gmt":"2022-08-17T10:19:00","guid":{"rendered":"https:\/\/wordpress.mediadoma.com\/?p=226023"},"modified":"2022-11-08T01:53:17","modified_gmt":"2022-11-07T22:53:17","slug":"mysql-i-tabeliloigud-ja-levinud-probleemide-parandused","status":"publish","type":"post","link":"https:\/\/wordpress.mediadoma.com\/et\/mysql-i-tabeliloigud-ja-levinud-probleemide-parandused\/","title":{"rendered":"MySQL-i tabelil\u00f5igud ja levinud probleemide parandused"},"content":{"rendered":"\n<p>MySQL-i tabelijupid?<\/p>\n<p>Jah, selles artiklis on neid \u00fcsna palju. Aga enne selle juurde j\u00f5udmist, mis on MySQL? Alustame MySQL-i m\u00e4\u00e4ratlusega. MySQL on Oracle&#8217;i toetatud avatud l\u00e4htekoodiga relatsioonilise andmebaasi halduss\u00fcsteem (RDBMS), mis p\u00f5hineb struktureeritud p\u00e4ringukeelel (SQL).<\/p>\n<p>MySQL-i andmebaasi kasutavad rakendused on TYPO3, MODx, Joomla, WordPress, Simple Machines Forum, phpBB, MyBB ja Drupal. MySQL-i kasutatakse ka paljudel k\u00f5rgetasemelistel suuremahulistel veebisaitidel, sealhulgas Google&#8217;is (kuigi mitte otsingute jaoks), Facebookis, Twitteris, Flickris ja YouTube&#8217;is.<\/p>\n<p>Ja p\u00e4rast kogu seda sissejuhatust m\u00f5istate, et MySQL on suurem, kui alguses arvasite. Kui te pole selles valdkonnas m\u00f5nda aega tegutsenud. Kui olete aga uustulnuk ja soovite MySQL-i kohta rohkem teada saada, on lihtsaim viis seda teha MySQL-i tabelijuppidega.<\/p>\n<h2>MySQL-i tabelijupid<\/h2>\n<p><strong>Sobitage string stringi alguses<\/strong><\/p>\n<p><code>SELECT 'Test' REGEXP '^The'; -- 0 SELECT 'The Test' REGEXP '^The'; -- 1<\/code><\/p>\n<p><strong>Kui nime ees ei ole &#8216;The&#8217;, siis lisage see<\/strong><\/p>\n<p><code>UPDATE [table] SET Name = CONCAT('The ', TRIM(Name)) WHERE Name NOT REGEXP '^The'<\/code><\/p>\n<p><strong>Kopeerige veerg \u00fchest tabelist teise<\/strong><\/p>\n<p><code>INSERT INTO [table] (<\/code><\/p>\n<p><code>) SELECT FROM [table]<\/code><\/p>\n<p><strong>Eemaldage k\u00f5ik t\u00fchikud<\/strong><\/p>\n<p><code>UPDATE [table] SET<\/code><\/p>\n<p><code>= REPLACE(, ' ', '')<\/code><\/p>\n<p><strong>(see on BASH) kukutage k\u00f5ik andmebaasi tabelid<\/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>Tabeli ja k\u00f5igi selle olemasolevate veergude v\u00f5rdlemise muutmine<\/strong><\/p>\n<p><code>alter table [table] convert to character set utf8 collate utf8_general_ci;<\/code><\/p>\n<p><strong>MySQL Otsi ja asenda<\/strong><\/p>\n<p><code>UPDATE files SET filepath = REPLACE(filepath,'path\/to\/search','path\/to\/replace');<\/code><\/p>\n<p><strong>Juhusliku rea valimine MySQL-is<\/strong><\/p>\n<p><code>SELECT column FROM table ORDER BY RAND() LIMIT 1<\/code><\/p>\n<p><strong>Looge MySQL-ist CSV<\/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>Otsige \u00fcles topeltkirjed<\/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>Vale m\u00e4rgikodeeringu parandamine MySQL-i andmetes<\/strong><\/p>\n<p><code>UPDATE table SET column=CONVERT(CONVERT(CONVERT(column USING latin1) USING binary) using utf8);<\/code><\/p>\n<p><strong>Laadige CSV tagasi mySQL-i<\/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 Otsige andmebaasist PHP-koodi<\/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>Looge MySQL-is juhuslik string<\/strong><\/p>\n<p><code>SELECT SUBSTRING(MD5(RAND()) FROM 1 FOR 6) AS randomstring<\/code><\/p>\n<p><strong>Arvutage vanus s\u00fcnnikuup\u00e4evast<\/strong><\/p>\n<p><code>SELECT FLOOR(DATEDIFF(CURRENT_DATE(), dob) \/ 365);<\/code><\/p>\n<p><strong>Otsi ja asenda<\/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>Valige kahe kuup\u00e4eva vahel<\/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>Varundage MySQL-i andmebaas SQL-faili rakendusega 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>kuidas arvutada viimast 7 p\u00e4eva<\/strong><\/p>\n<p><code>WHERE mydatefld &gt;= DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY)<\/code><\/p>\n<p><strong>mysql dump<\/strong><\/p>\n<p><code>mysqldump -u [username] -p [password] [databasename] &gt; [backupfile.sql]<\/code><\/p>\n<p><strong>Mysql asendamine<\/strong><\/p>\n<p>`# Asenda string k\u00f5igis tabeli kirjetes<br \/>\nUPDATE tabeli_nimi SET veeru_nimi = REPLACE( veeru_nimi, &#8216;\/dev&#8217;, &quot; );<\/p>\n<p>SELECT REPLACE( veeru_nimi, &#8216;\/dev&#8217;, &#8220;) FROM tabeli_nimi;`<\/p>\n<p><strong>Teisendage ajav\u00f6\u00f6nd kohalikuks<\/strong><\/p>\n<p><code>SELECT CONVERT_TZ(mydatefield,'US\/Pacific','CET') FROM mytable;<\/code><\/p>\n<p><strong>Otsige ja asendage mitmel real SQL-is<\/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>Kuidas salvestada MySQL-i tulemusi v\u00e4lisesse faili<\/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>Sisestage v\u00f5i v\u00e4rskendage, kui kordumatu v\u00f5ti on juba olemas<\/strong><\/p>\n<p><code>INSERT INTO `tableName` VALUES (?,?,?,'-1',?,'0') ON DUPLICATE KEY UPDATE `id` = ?, `foo` = ?, `bar` = ?<\/code><\/p>\n<p><strong>mySQL \u2013 andmebaasi varundamise k\u00e4sud<\/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>MySql import<\/strong><\/p>\n<p><code>mysql -u [username] -p [dbname] &lt; [filename].sql<\/code><\/p>\n<p><strong>leida topeltkirjeid<\/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 Otsi ja asenda<\/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>Muutke Drupali faili kataloogi teed<\/strong><\/p>\n<p><code>UPDATE FILES SET filepath = REPLACE(filepath, 'sites\/default\/files\/old\/','sites\/default\/files\/new\/');<\/code><\/p>\n<p><strong>Otsige MySQL andmebaasist \u00fcles k\u00f5ik tabelid, mis sisaldavad konkreetseid veergude nimesid<\/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>Otsige tabelist v\u00e4\u00e4rtused, mis esinevad t\u00e4pselt \u00fcks kord<\/strong><\/p>\n<p><code>SELECT email FROM users GROUP BY email HAVING (COUNT(email) = 1 )<\/code><\/p>\n<p><strong>MySql skript kehtetute e-posti aadresside leidmiseks<\/strong><\/p>\n<p><code>SELECT * FROM people WHERE email NOT LIKE '%_@__%.__%'<\/code><\/p>\n<p><strong>MySQL-i vanuse arvutamine<\/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>Vasakpoolne liitmine kolme v\u00f5i enama tabeli linkimiseks<\/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 vaste komadega eraldatud loend<\/strong><\/p>\n<p><code>WHERE id REGEXP '(,|^){$this-&gt;id}(,|$)'<\/code><\/p>\n<p><strong>Hankige kaalu p\u00f5hjal juhuslik rekord<\/strong><\/p>\n<p><code>SELECT * FROM Table ORDER BY Rand() * (1 \/ Weight<\/code><\/p>\n<p>Loodame, et need MySQL-i tabelil\u00f5igud on see, mida otsisite. V\u00e4rskendame seda artiklit teiste MySQL-i fragmentidega, millega tulevikus kokku puutume, seega lisage see kindlasti j\u00e4rjehoidjatesse.<\/p>\n<p>Kui teile meeldis lugeda seda artiklit MySQL-i tabelijuppide kohta, peaksite lugema ka j\u00e4rgmist:<\/p>\n<ul>\n<li><a href=\"https:\/\/wordpress.mediadoma.com\/et\/crud-suesteemi-loomine-wordpressis-wpdatatables-tables-plugin\/\" title=\"CRUD-s\u00fcsteemi loomine WordPressis (2018)\" >CRUD-s\u00fcsteemi loomine WordPressis (2018)<\/a><\/li>\n<li><a href=\"https:\/\/wordpress.mediadoma.com\/et\/12-naepunaeidet-eduka-wordpressi-teema-voi-pistikprogrammi-loomiseks\/\" title=\"12 n\u00e4pun\u00e4idet eduka WordPress Premiumi pistikprogrammi v\u00f5i teema loomiseks\" >12 n\u00e4pun\u00e4idet eduka WordPress Premiumi pistikprogrammi v\u00f5i teema loomiseks<\/a><\/li>\n<li><a href=\"https:\/\/wpdatatables.com\/how-to-host-a-website\/\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">Kuidas veebisaiti majutada: k\u00f5ik sammud on selgitatud<\/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\">WordPressi WP_OPTIONS tabeli optimeerimine<\/a><\/li>\n<\/ul>\n<p><div id=\"PostUnique_PostSource\" style=\"padding-top: 50px\">:  <a target=\"_blank\" rel=\"noopener nofollow\" href=\"\/\/wpdatatables.com\" class=\"external external_icon\">wpdatatables.com<\/a><\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL-i tabelijupid? Jah, selles artiklis on neid \u00fcsna palju. Aga enne selle juurde j\u00f5udmist, mis on MySQL? Alustame MySQL-i m\u00e4\u00e4ratlusega.<\/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":[833,842,863],"tags":[1165],"class_list":["post-226023","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-juhend-algajatele","category-opetused","category-wordpress-4","tag-affiai-et"],"_links":{"self":[{"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/posts\/226023","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/comments?post=226023"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/posts\/226023\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/media\/206960"}],"wp:attachment":[{"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/media?parent=226023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/categories?post=226023"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/et\/wp-json\/wp\/v2\/tags?post=226023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}