{"id":111,"date":"2012-04-05T04:25:33","date_gmt":"2012-04-05T04:25:33","guid":{"rendered":"http:\/\/clay2x.wordpress.com\/?page_id=111"},"modified":"2012-04-05T04:25:33","modified_gmt":"2012-04-05T04:25:33","slug":"mysql","status":"publish","type":"page","link":"https:\/\/potterdreams.com\/clay\/interest\/programmings\/mysql\/","title":{"rendered":"MYSQL"},"content":{"rendered":"<h2>Note: All these based on MySQL 5.1.61<\/h2>\n<h2>1.\u00a0Find duplicate entries in table<\/h2>\n<p>SELECT COUNT(*), column1, column2 FROM tablename<br \/>\nGROUP BY column1, column2<br \/>\nHAVING COUNT(*)&gt;1;<\/p>\n<h2>1b. List Down all the duplicate entries<\/h2>\n<p><span style=\"font-size: 12px\">SELECT userid, users.user_ic, useremail, company FROM `users` INNER JOIN (SELECT useric FROM users WHERE useric &lt;&gt; &#8220;&#8221; GROUP BY `useric` HAVING COUNT(*)&gt;1) dup ON users.user_ic = dup.user_ic order by dup.useric<\/span><\/p>\n<div><\/div>\n<h2>2.\u00a0Find entries that are <em>null<\/em>\u00a0id on the other table<\/h2>\n<p>SELECT * FROM table1 LEFT JOIN table2 ON (table1.id = table2.ID) WHERE table2.ID IS NULL<\/p>\n<h2>3.\u00a0Find distinct\/unique records<\/h2>\n<p>SELECT <em>DISTINCT<\/em> column1, column2 FROM table ORDER BY column1, column2<\/p>\n<h2>4. SELECT whether value exists among many possible values<\/h2>\n<p>SELECT * FROM table1 WHERE value NOT IN (&#8216;value1&#8217;, &#8216;value2&#8217;, &#8230;)<\/p>\n<div><\/div>\n<h2>4.\u00a0Joining \/ combining 2 tables with same structure<\/h2>\n<p>SELECT * FROM table1 UNION SELECT * FROM table2<br \/>\nNote: Able to CREATE VIEW using the above syntax to form new VIEW<\/p>\n<h2>5. Create &amp; populate table from a VIEW<\/h2>\n<p>CREATE TABLE table AS SELECT * FROM view;<br \/>\nINSERT INTO table SELECT * FROM view;<\/p>\n<h2>6.\u00a0Read UTF-8 in MySQL<\/h2>\n<p><span style=\"color: #000000\"><span style=\"line-height: 32px\">Put this line before any query<\/span>:<br \/>\n<em><span style=\"line-height: 32px\">mysql_query(&#8220;SET character_set_results=utf8&#8221;, $dbconn);\u00a0<\/span><\/em><\/span><\/p>\n<h2>7. Create Trigger to update another table in different database<\/h2>\n<p>CREATE TRIGGER <em>trigger_name<\/em> BEFORE INSERT ON <em>table_name<br \/>\n<\/em>FOR EACH ROW<br \/>\nINSERT INTO db2.table2 (col1,col2,datetime,&#8230;) VALUES (NEW.col1,NEW.col2,SYSDATE(),&#8230;);<\/p>\n<p><span style=\"color: #000000\">Alternatively, for multiple actions, need to add the following items:<\/span><\/p>\n<p><strong>DELIMITER |<\/strong><\/p>\n<p>CREATE TRIGGER\u00a0<em>trigger_name<\/em>\u00a0BEFORE INSERT ON\u00a0<em>table_name<\/em><br \/>\nFOR EACH ROW<\/p>\n<p><strong>BEGIN<\/strong><br \/>\nstmt1;<br \/>\nstmt2;<br \/>\nSET NEW.col3=(SELECT col FROM <em>table1<\/em> WHERE <em>col2<\/em>=NEW.<em>col2<\/em>);<br \/>\n<strong>END;<br \/>\n|<\/strong><\/p>\n<p><strong>DELIMITER ;<\/strong><\/p>\n<p>Thanks to John Hundley,\u00a0Jonathan Haddad, MySQL \ud83d\ude42<br \/>\nRead for further readings on Trigger:<\/p>\n<ol>\n<li><a title=\"Forum1\" href=\"http:\/\/forums.mysql.com\/read.php?99,390512,397560#msg-397560\" target=\"_blank\" rel=\"noopener noreferrer\">Forum1<\/a><\/li>\n<li><a title=\"Artful Software\" href=\"http:\/\/www.artfulsoftware.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">MySQL Expert<\/a><\/li>\n<li><a title=\"IfElseCondition\" href=\"http:\/\/forums.mysql.com\/read.php?99,159252,159252\" target=\"_blank\" rel=\"noopener noreferrer\">With If Else Condition<\/a><\/li>\n<li><a title=\"MoreExamples\" href=\"http:\/\/mysqldatabaseadministration.blogspot.com\/2006\/01\/can-mysql-triggers-update-another.html\" target=\"_blank\" rel=\"noopener noreferrer\">More Examples<\/a><\/li>\n<li><a title=\"Complement\" href=\"http:\/\/forums.mysql.com\/read.php?99,161909,162074#msg-162074\" target=\"_blank\" rel=\"noopener noreferrer\">Interesting Complementary<\/a><\/li>\n<li><a title=\"MySQL Help\" href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.0\/en\/create-trigger.html\" target=\"_blank\" rel=\"noopener noreferrer\">Default Help<\/a><\/li>\n<li><a title=\"Rusty Razor\" href=\"http:\/\/www.rustyrazorblade.com\/2006\/09\/mysql-triggers-tutorial\/comment-page-1\/#comments\" target=\"_blank\" rel=\"noopener noreferrer\">Another interesting read<\/a><\/li>\n<li><a title=\"Detail help\" href=\"http:\/\/forge.mysql.com\/wiki\/Triggers\" target=\"_blank\" rel=\"noopener noreferrer\">Very detailed help<\/a><\/li>\n<li><a title=\"From India\" href=\"http:\/\/www.roseindia.net\/mysql\/mysql5\/triggers.shtml\" target=\"_blank\" rel=\"noopener noreferrer\">Similar reference<\/a><\/li>\n<\/ol>\n<h2>8. DELETE records on N join tables<\/h2>\n<p>DELETE table1, table2, &#8230; FROM table1 JOIN table2 ON () WHERE <em>condition<\/em><\/p>\n<h5><span style=\"color: #000000;font-size: 1.8em;line-height: 1.5em\">9. UPDATE COLUMN based on results of SELECT statement<\/span><\/h5>\n<p>UPDATE <em>table_A<\/em> (INNER JOIN (SELECT <em>table_A<\/em>.id as <em>reg_id<\/em>, <em>table_B<\/em>.target as <em>pf<\/em> FROM <em>table_A<\/em> LEFT JOIN <em>table_B<\/em> ON (<strong>table_A<\/strong>.<em>a_id<\/em> = <strong>table_B<\/strong>.b_id)) as <em>current_select<\/em> SET <em>table_A<\/em>.target = <em>current_select<\/em>.pf WHERE <em>table_A<\/em>.id = <em>current_select<\/em>.reg_id<\/p>\n<p>or probably this could work also (haven&#8217;t tried)<\/p>\n<p>*UPDATE table_a SET column_a1 = (SELECT column_b1 FROM table_b<br \/>\nWHERE table_b.column_b3 = table_a.column_a3);<\/p>\n<p>*taken from Karlsson blog at:\u00a0<a title=\"Karlsson Blog\" href=\"http:\/\/karlssonondatabases.blogspot.sg\/2009\/01\/multicolumn-update-with-subquery-mysql.html\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/karlssonondatabases.blogspot.sg\/2009\/01\/multicolumn-update-with-subquery-mysql.html<\/a><\/p>\n<h2>10. UPDATE COLUMN with VALUE from Another Table<\/h2>\n<p>UPDATE <em>table_b<\/em> INNER JOIN <em>table_a<\/em> ON (<em>table_a.id = table_b.id<\/em>) SET <em>table_b.name = table_a.name<\/em> WHERE <em>table_a.id &gt; x<\/em><\/p>\n<h2>11. UPDATE COLUMN replacing PART of String<\/h2>\n<p>UPDATE\u00a0<em>table_A<\/em>\u00a0\u00a0SET <em>url<\/em>\u00a0= <em>REPLACE (url, &#8216;domain1&#8217;, &#8216;domain2&#8217;)\u00a0<\/em>WHERE <em>url<\/em>\u00a0like <em>&#8216;%domain1%&#8217;<\/em><\/p>\n<div>\n<h2>12. GROUP COLUMN &amp; Count<\/h2>\n<p>SELECT \u00a0columnA AS `Col A`, COUNT( DISTINCT user_id) AS Count FROM A JOIN B ON (A.id = B.id) \u00a0WHERE (A.col2 &lt;&gt; &#8220;x&#8221;) GROUP BY user_type, columnA order by start_date<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Note: All these based on MySQL 5.1.61 1.\u00a0Find duplicate entries in table SELECT COUNT(*), column1, column2 FROM tablename GROUP BY column1, column2 HAVING COUNT(*)&gt;1; 1b. List Down all the duplicate entries SELECT userid, users.user_ic, useremail, company FROM `users` INNER JOIN &hellip; <a href=\"https:\/\/potterdreams.com\/clay\/interest\/programmings\/mysql\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"parent":103,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"class_list":["post-111","page","type-page","status-publish","hentry"],"jetpack-related-posts":[{"id":724,"url":"https:\/\/potterdreams.com\/clay\/pages\/2014-thanksgivings\/","url_meta":{"origin":111,"position":0},"title":"2014 Thanksgivings","author":"clay","date":"January 29, 2014","format":false,"excerpt":"This year, I thought to start it early and regularly: Our health: For 3 weeks our family went down with sickness. Starting from our girl, then my wife, and lastly the worst was me. We \"wasted\" New Year and my wife's birthday on sickness. My sister-in-law bought my wife birthday\u2026","rel":"","context":"Similar post","block_context":{"text":"Similar post","link":""},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/potterdreams.com\/clay\/wp-json\/wp\/v2\/pages\/111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/potterdreams.com\/clay\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/potterdreams.com\/clay\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/potterdreams.com\/clay\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/potterdreams.com\/clay\/wp-json\/wp\/v2\/comments?post=111"}],"version-history":[{"count":0,"href":"https:\/\/potterdreams.com\/clay\/wp-json\/wp\/v2\/pages\/111\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/potterdreams.com\/clay\/wp-json\/wp\/v2\/pages\/103"}],"wp:attachment":[{"href":"https:\/\/potterdreams.com\/clay\/wp-json\/wp\/v2\/media?parent=111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}