{"id":83,"date":"2010-02-14T04:09:52","date_gmt":"2010-02-14T04:09:52","guid":{"rendered":"http:\/\/xbmc\/wordpress\/?p=83"},"modified":"2012-03-12T11:44:15","modified_gmt":"2012-03-12T16:44:15","slug":"mysql-tricks","status":"publish","type":"post","link":"https:\/\/xfloyd.net\/blog\/?p=83","title":{"rendered":"MySQL tricks"},"content":{"rendered":"<p>The LENGTH() function in MySQL returns the length of a string in bytes. Multi-byte characters will count as multiple bytes. The examples in this post will use the LENGTH() function but you can substitute them with the CHAR_LENGTH() function instead if you want to count the number of characters rather than bytes. Note that CHAR_LENGTH will treat a character that uses two bytes a one single character and would return 1.<!--more--><\/p>\n<p>The first example below illustrates what LENGTH() will return:<\/p>\n<pre>SELECT LENGTH('this is a test');<\/pre>\n<p>This will return the number 14.<\/p>\n<p>To work out the length of the largest string in a MySQL table, combine the LENGTH() and MAX() functions together like so:<\/p>\n<pre>SELECT MAX(LENGTH(field_to_query)) FROM table_to_query;<\/pre>\n<p>where &#8220;field_to_query&#8221; is the fieldname you want to query and table_to_query is the table. This will then return the length of the longest field named &#8220;field_to_query&#8221; as an integer.<\/p>\n<p>This can be quite useful if you&#8217;ve loaded data from an external source into a table and want to ensure you have enough storage space for the data. I need to regularly load data from a text file into a MySQL table and one of the fields is labelled &#8220;long_description&#8221;. Initially I created the field as a VARCHAR(255) and then ran the above query against the table only to discover the longest field(s) were 255 bytes long. I then changed the table to type TEXT and ran the query again and found out the longest field was acutally 1210 bytes long.<\/p>\n<p>This final example will show all the unique lengths along with a count:<\/p>\n<pre>SELECT LENGTH(field_to_query), COUNT(*)\r\nFROM table_to_query GROUP BY LENGTH(field_to_query);<\/pre>\n<p>To order it by the those with the highest count first, add &#8220;ORDER BY COUNT(*) DESC&#8221; and to order it by the longest count first, add &#8220;ORDER BY LENGTH(field_to_query) DESC&#8221; e.g.:<\/p>\n<pre>SELECT LENGTH(field_to_query), COUNT(*)\r\nFROM table_to_query GROUP BY LENGTH(field_to_query)\r\nORDER BY COUNT(*) DESC<\/pre>\n<pre>SELECT LENGTH(long_description), COUNT(*)\r\nFROM table_to_query GROUP BY LENGTH(field_to_query)\r\nORDER BY LENGTH(field_to_query) DESC<\/pre>\n<p>UPDATE store_list SET customer_id = REPLACE(customer_id, \u201809-\u2019, \u201802-\u2019) LIMIT 1<br \/>\nSELECT REPLACE(\u2019www.mysql.com\u2019, \u2018w\u2019, \u2018Ww\u2019);<\/p>\n<p>Above statement will return \u2018WwWwWw.mysql.com\u2019 as result.<\/p>\n<p>WHERE somefileld IN (1,2,3)<\/p>\n<p>LOAD DATA LOCAL INFILE &#8216;\/directory.csv&#8217; INTO TABLE `directory_data` FIELDS TERMINATED BY &#8216;,&#8217; ENCLOSED BY &#8216;&#8221;&#8216; ESCAPED BY &#8216;\\&#8217; LINES TERMINATED BY &#8216;rn'(`cat_name` , `item_name` , `phone` , `address` , `zip_code` )<!--more--><\/p>\n<p>mysql -u phpmyadmin -p -h localhost mipueblo_data<\/p>\n<p>where you need to replace domain with your actual domain and example.org with correct domain and .tld.<\/p>\n<p>-u is the switch to specify the user. Your MySQL username is xdomain where domain is your domain.<\/p>\n<p>-p tells MySQL to prompt for a password<\/p>\n<p>-h tells MySQL what host to use. Your host is mysql.example.org (replace with your actual domain and .tld)<\/p>\n<p>xdatabase in the above command line instruction is the name of the database.<\/p>\n<p>SELECT * FROM `directory_data` WHERE cat_name =&#8217;CARNIVALS&#8217;;<br \/>\nSELECT user_email, count( user_email )<br \/>\nFROM phpbb_users<br \/>\nGROUP BY user_email<br \/>\nHAVING count( * ) &gt;1 LIMIT 0 , 30<\/p>\n<p>The most normal use of mysqldump is probably for making a backup of whole databases. See section 4.4.1 Database Backups.<\/p>\n<p>mysqldump &#8211;opt database &gt; backup-file.sql<br \/>\nYou can read this back into MySQL with:<\/p>\n<p>mysql -u user -p database &lt; backup-file.sql<\/p>\n<p>rsync -a \/home\/webadmin\/ \/backups\/<br \/>\ntar jcf \/home\/webadmin\/backup.tar.bz2 \/backups<\/p>\n<p>Select<br \/>\nReturn all records all columns in a table:<\/p>\n<p>select * from TableName<br \/>\nReturn all records but only field1 and field2 in a table:<\/p>\n<p>select field1, field2 from TableName<br \/>\nReturn field1 for all records in a table with a specific value for field2:<\/p>\n<p>select field1 from TableName where field2=123<br \/>\nReturn the number of records in a table:<\/p>\n<p>select count(*) from TheTable<br \/>\nReturn the number of records in a table with a specific value for field2:<\/p>\n<p>select count(*) from TableName where field2=123<br \/>\nSimple join:<\/p>\n<p>select * from table1, table2<br \/>\nwhere table1.field1=table2.fieldA<br \/>\nor<\/p>\n<p>select table1.field1, table2.fieldA from table1, table2<br \/>\nwhere table1.field2=table2.fieldB<\/p>\n<p>Select all unique values in field1 from a table:<\/p>\n<p>select distinct(field1) from TableName<br \/>\nor<\/p>\n<p>select distinct field1 from TableName<br \/>\nSelect the number of unique values:<\/p>\n<p>select count(*) from<br \/>\n(select distinct field1 from TableName)<br \/>\nSelect all duplicate records in a table, where two (or more) records are considered duplicates if they share a common value for a single field:<\/p>\n<p>select field, count(field) from TableName<br \/>\ngroup by field<br \/>\nhaving count(*) &gt; 1<br \/>\nSelect all duplicate records in a table, where two (or more) records are considered duplicates if they share common values for a pair of fields:<\/p>\n<p>select field1, field2, count(*) from TableName<br \/>\ngroup by field1, field2<br \/>\nhaving count(*) &gt; 1<br \/>\nSelect similar records, i.e. all records which have duplicate field1 and field2 in a table but with different field3 (i.e. specifying which fields must be the same and which different):<\/p>\n<p>select * from table as A, table as B<br \/>\nwhere A.field1=B.field1<br \/>\nand A.field2=B.field2<br \/>\nand A.field3&lt;&gt;B.field3;<br \/>\nNote:<\/p>\n<p>It is important to specify at least one field which is different between the two records otherwise this query will list a record as being the same as itself.<br \/>\nThis query will not find duplicate records, i.e. records with every field the same.<br \/>\nSelect all records from a table which do not share a common ID with records from a second table:<\/p>\n<p>select * from table1<br \/>\nwhere field1 not in (select field2 from table2)<br \/>\nNote: Sub-queries are quite slow and are not supported in MySQL, so the above will not work in MySQL.<\/p>\n<p>or an alternative using a join (which can be much faster):<\/p>\n<p>select table1.* from table1<br \/>\nleft join table2 on (table1.field1 = table2.field2)<br \/>\nwhere table2.field2 is null;<\/p>\n<p>The following method (which has been suggested by Michael Miller) is to use EXISTS. It is much faster on SQL Server than the above (but Michael says it is comparable with the left join technique on Oracle):<\/p>\n<p>select * from table1<br \/>\nwhere not exists (select field2 from table2 where table2.field2 = table1.field1)<\/p>\n<p>To perform a two way join:<\/p>\n<p>select * from<br \/>\ntable1 left join table2 on (table1.field1 = table2.field1),<br \/>\ntable1 left join table3 on (table1.field2 = table3.field3)<\/p>\n<p>this has been tested on SQL Server, but not on Oracle or MySql. It does not work with MS-Access.<\/p>\n<p>To combine the results of two queries (be aware that the number and types of fields in both queries must agree):<\/p>\n<p>select * from table1<br \/>\nunion select * from table2<br \/>\nTo return a value based on the contents of a field. This can be done using either Iif, Decode or Case, depending on the database.<\/p>\n<p>The following works with MSAccess:<\/p>\n<p>select Iif(field1 = 1, &#8216;one&#8217;, &#8216;not one&#8217;)<br \/>\nfrom TableName<br \/>\nThis is equivalent to the following on SqlServer:<\/p>\n<p>select Case field1 = 1 then &#8216;One&#8217; else &#8216;Two&#8217; End<br \/>\nfrom TableName<br \/>\nFor Oracle use the DECODE function.<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>Insert<br \/>\nInsert new record into a table:<\/p>\n<p>insert into TableName values (1,2,3)<br \/>\nInsert new record into a table explicitly naming fields:<\/p>\n<p>insert into TableName (field1,field2,field3) values (1,2,3)<\/p>\n<p>Insert new record into a table using values from another table:<\/p>\n<p>insert into TableName (field1,field2,field3)<br \/>\nselect fieldA,2,fieldC from SomeTable<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>Update<br \/>\nUpdate all records in a table:<\/p>\n<p>update TableName set field1=2<br \/>\nUpdate specific records in a table:<\/p>\n<p>update TableName set field1=2 where field1=1<br \/>\nTo update more than one field at a time:<\/p>\n<p>update TableName set field1=2, field2=3<br \/>\nUpdate a field in a table using a value from another table where both records are referenced by a common key:<\/p>\n<p>update TableOne<br \/>\ninner join TableTwo on TableOne.commonID = TableTwo.commonID<br \/>\nset TableOne.field1 = TableTwo.fieldX<\/p>\n<p>or<\/p>\n<p>update TableOne, TableTwo<br \/>\nset TableOne.field1 = TableTwo.fieldX<br \/>\nwhere TableOne.commonID = TableTwo.commonID<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>Delete<br \/>\nDelete all records in a table (dangerous):<\/p>\n<p>delete from TableName<br \/>\nDelete specific records in a table:<\/p>\n<p>delete from TableName where field1=1<br \/>\nDelete records from one table which do not have a matching field in another table:<\/p>\n<p>delete from TableName where field1 not in<br \/>\n(select field2 from TableTwo)<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>Keys<br \/>\nBe aware that there are often subtle syntax variations between different database systems. Also other key properties (for example &#8216;clustered&#8217;) will vary between database systems. Therefore please treat this part of the SQL crib sheet as a guide only.<\/p>\n<p>Create a primary key on a table:<\/p>\n<p>Alter Table TheTable Add Primary Key (field1, field2)<\/p>\n<p>To add an index on a field:<\/p>\n<p>alter table TableName Add Index (field1)<br \/>\nTo remove a primary key:<\/p>\n<p>alter table drop primary key<br \/>\nCreating a Crontab<br \/>\nWhat is Crontab?<br \/>\nCrontab is a program that allows users to create jobs that will run at a given time. Each individual user has their own crontab and the entire system has a crontab that can only be modified by those with root access.\u00a0 If you are adding a crontab for an individual user you must sign on as that user first.\u00a0 (ex. su userid)<\/p>\n<p>The syntax of this file is very rigid. There are six fields to a file, each separated by a space. The first five fields specify exactly when the command is to be run; the sixth field is the command itself. The first five fields are:<\/p>\n<p>Format<br \/>\nminute hour day month weekday command<br \/>\nMinute &#8211; Minutes after the hour (0-59).<br \/>\nHour &#8211; 24-hour format (0-23).<br \/>\nDay &#8211; Day of the month (1-31).<br \/>\nMonth &#8211; Month of the year (1-12).<br \/>\nWeekday &#8211; Day of the week. (0-6; the 0 refers to Sunday).<\/p>\n<p>Asterisks (*) specify when commands are to be run in every instance of the value of the field. For instance, an asterisk in the Month field would mean that the command should be run every month. In addition, multiple events can be scheduled within a field by separating all instances with commas &#8211; with no space between.<\/p>\n<p>Options<br \/>\ncrontab -e<br \/>\nEdits the current crontab or creates a new one. *<\/p>\n<p>crontab -l<br \/>\nLists the contents of the crontab file.<\/p>\n<p>crontab -r<br \/>\nRemoves the crontab file.<\/p>\n<p>Creating a Crontab for use with AccessWatch<br \/>\nYou must first sign on as the individual user you are adding the crontab for.\u00a0 Do not create crontab while you are signed on as &#8220;su&#8221; under root or you will change the root crontab.\u00a0 After you sign on as root su as individual user.<br \/>\nsu userid\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ex.\u00a0 su nercols<\/p>\n<p>Change editor to Joe from VI:<\/p>\n<p>EDITOR=joe<br \/>\nexport EDITOR<\/p>\n<p>Create the crontab from the command line:<\/p>\n<p>crontab -e<\/p>\n<p>Insert the following lines and the save the file (Ctrl-kx):<\/p>\n<p>30 23 * * * \/home\/userid\/www\/aw\/aw-parser.pl -d<br \/>\n45 23 * * * \/home\/userid\/www\/aw\/aw-report.pl<\/p>\n<p>AccessWatch will run against the log files in their home directory every evening at 11:30 and then generate a report at 11:45 p.m..<\/p>\n<p>Type man crontab for more detailed information.\u00a0 If you don&#8217;t want to set up a crontab just run the by hand when you want a report.<\/p>\n<p>.\/aw-parser.pl<br \/>\n.\/aw-report.pl<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The LENGTH() function in MySQL returns the length of a string in bytes. Multi-byte characters will count as multiple bytes. The examples in this post will use the LENGTH() function but you can substitute them with the CHAR_LENGTH() function instead if you want to count the number of characters rather than bytes. Note that CHAR_LENGTH [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/83"}],"collection":[{"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=83"}],"version-history":[{"count":4,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/83\/revisions"}],"predecessor-version":[{"id":567,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/83\/revisions\/567"}],"wp:attachment":[{"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=83"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=83"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=83"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}