{"id":32,"date":"2012-07-10T09:54:37","date_gmt":"2012-07-10T09:54:37","guid":{"rendered":"http:\/\/codeatelier.wordpress.com\/?p=32"},"modified":"2016-09-10T13:54:28","modified_gmt":"2016-09-10T13:54:28","slug":"mysql-queries","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/mysql-queries\/","title":{"rendered":"Mysql Queries"},"content":{"rendered":"<p><strong>1.How To Create an SNo In MySql &#8211; Similar to row_num in MSSQL<\/strong><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT @rownum:= @rownum+1 AS Sno\r\n  FROM Personal_details p,(SELECT @rownum:=0) r;\r\n<\/pre>\n<p><strong>How to Subtract 7 Day from Now<\/strong><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT DATE_SUB(NOW(),INTERVAL 7 DAY)&lt;\/pre&gt;\r\n<\/pre>\n<p><strong>How to View Active Connections &amp; Process<\/strong><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSHOW PROCESSLIST;\r\n<\/pre>\n<p><strong>How to Get Balance<\/strong><br \/>\n<strong>Input Table<\/strong><br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/codeatelier.files.wordpress.com\/2012\/07\/1.gif\" alt=\"\" width=\"233\" height=\"153\" \/><\/p>\n<p><strong>Query 1<\/strong><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n    SELECT date,\r\n          SUM(CASE\r\n               WHEN TYPE = 'cash_in' THEN amount\r\n               END) AS cash_in,\r\n          SUM(CASE\r\n                 WHEN TYPE = 'cash_out' THEN amount\r\n               END) AS cash_out,\r\n          SUM(CASE\r\n                WHEN TYPE = 'cash_in' THEN amount\r\n                ELSE -amount\r\n               END) AS balance\r\n    FROM tbSample4\r\n   WHERE type IN ('cash_in', 'cash_out') \r\n   GROUP BY date(date)\r\n<\/pre>\n<p><strong>Query 2<\/strong><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n SELECT a.currency_id, cash_in, cash_out, (cash_in - cash_out) balance\r\n   FROM (SELECT currency_id, SUM(AMOUNT) cash_in\r\n           FROM tbSample4\r\n          WHERE type = 'cash_in'\r\n          GROUP BY currency_id) a,\r\n        (SELECT currency_id, SUM(AMOUNT) cash_out\r\n           FROM tbSample4\r\n          WHERE type = 'cash_out'\r\n          GROUP BY currency_id) b\r\n  WHERE a.currency_id = b.currency_id\r\n<\/pre>\n<p><strong>Output Table<\/strong><br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/codeatelier.files.wordpress.com\/2012\/07\/2.gif\" alt=\"\" width=\"282\" height=\"90\" \/><\/p>\n<p><strong>2.How to Make Selected Names at the Top of List<\/strong><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n SELECT Names\r\n   FROM tbNames&lt;\/pre&gt;\r\n\r\n&lt;strong&gt;Input Table&lt;\/strong&gt;\r\n&lt;img src=&quot;http:\/\/codeatelier.files.wordpress.com\/2012\/07\/6.jpg&quot; alt=&quot;&quot; width=&quot;70&quot; height=&quot;235&quot; \/&gt;\r\n\r\n&lt;strong&gt;Output&lt;\/strong&gt;\r\n\r\n&lt;strong&gt;Query1&lt;\/strong&gt;\r\n&#x5B;code lang=&quot;sql&quot;]\r\nSELECT Names\r\n  FROM tbNames\r\n ORDER BY CASE WHEN names IN ('Chandiran', 'Suryan') \r\n               THEN 0\r\n               ELSE 1\r\n               END&lt;\/pre&gt;\r\n<\/pre>\n<p><strong>Query2<\/strong><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n SELECT * \r\n   FROM tbNames\r\n  ORDER BY  Names IN ('Heman','superman') DESC;&lt;\/pre&gt;\r\n&lt;img src=&quot;http:\/\/codeatelier.files.wordpress.com\/2012\/07\/7.jpg&quot; alt=&quot;&quot; width=&quot;75&quot; height=&quot;241&quot; \/&gt;\r\n<\/pre>\n<p><strong>3.How To Return Row in Column as Comma Separated Value CSV<\/strong><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\"> \r\n   SELECT GROUP_CONCAT(Name)\r\n     FROM tblNames\r\n    WHERE Id IN(15,16,17,18);\r\n<\/pre>\n<p><strong>4.How Add OR Condition in Mysql case<\/strong><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nCREATE TABLE tblConfirmationStatus (Confirm_Status TINYINT);\r\n\r\nINSERT INTO tblConfirmationStatus \r\nConfirm_Status\r\nVALUES\r\n(1),\r\n(0),\r\n(1),\r\n(null),\r\n(0),\r\n(1),\r\n(null);\r\n<\/pre>\n<p><strong>Required Output<\/strong><br \/>\nConfirmStatus<\/p>\n<pre>   Confirmed\r\n   Not Confirmed\r\n   Confirmed\r\n   Not Confirmed\r\n   Not Confirmed\r\n   Confirmed\r\n   Not Confirmed\r\n<\/pre>\n<p><strong>Query 1<\/strong><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n SELECT CASE \r\n        WHEN Confirm_Status IS NULL OR Confirm_Status = 0 THEN 'Not Confirmed' \r\n        ELSE  'Confirmed' \r\n       END AS ConfirmStatus\r\n  FROM tblConfirmationStatus;\r\n<\/pre>\n<p><strong>Query 2<\/strong><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n SELECT CASE \r\n        WHEN Confirm_Status &amp;gt; 0 THEN 'Confirmed' \r\n        ELSE  'Not Confirmed' \r\n       END AS ConfirmStatus\r\n  FROM tblConfirmationStatus;\r\n<\/pre>\n<p><strong>MySQL Query To Skip Days &#8211; Sunday as Below<\/strong><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nCREATE TABLE Patients_Appointment_List(Patent_Id INT  UNSIGNED PRIMARY KEY AUTO_INCREMENT,Patient_Name VARCHAR(255),appointment_Date DATE);\r\n\r\nINSERT INTO Patients_Appointment_List\r\n (Patient_Name, appointment_Date)\r\n  VALUES\r\n ('Mugil', 20120730),\r\n ('Vinu',  20120730),\r\n ('Madhavan',  20120729),\r\n ('Gopal',  20120730),\r\n ('Shivaji',  20120731);\r\n\r\nSELECT Patient_Name\r\n  FROM Patients_Appointment_List\r\n WHERE CASE WHEN WEEKDAY(CURDATE() + INTERVAL 1 DAY) = 6 THEN \r\n       appointment_Date BETWEEN NOW() AND NOW() + INTERVAL 2 DAY \r\n            ELSE appointment_Date BETWEEN NOW() AND NOW() + INTERVAL 1 DAY\r\n       END\r\n<\/pre>\n<p><strong>Output as on 20120728<\/strong><br \/>\n<strong>Patient_Name<\/strong><\/p>\n<pre>\r\nMugil\r\nVinu\r\nMadhavan\r\nGopal\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1.How To Create an SNo In MySql &#8211; Similar to row_num in MSSQL SELECT @rownum:= @rownum+1 AS Sno FROM Personal_details p,(SELECT @rownum:=0) r; How to Subtract 7 Day from Now SELECT DATE_SUB(NOW(),INTERVAL 7 DAY)&lt;\/pre&gt; How to View Active Connections &amp; Process SHOW PROCESSLIST; How to Get Balance Input Table Query 1 SELECT date, SUM(CASE WHEN&hellip; <a href=\"https:\/\/codethataint.com\/blog\/mysql-queries\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,20],"tags":[],"class_list":["post-32","post","type-post","status-publish","format-standard","hentry","category-mysql","category-queries"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/32","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=32"}],"version-history":[{"count":2,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/32\/revisions"}],"predecessor-version":[{"id":1578,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/32\/revisions\/1578"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=32"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=32"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=32"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}