<?xml version="1.0" encoding="utf-8"?>
			
			<rss version="2.0" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cc="http://web.resource.org/cc/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">

			<channel>
			<title>Making It Up As I Go Along - mySQL</title>
			<link>http://www.miuaiga.com/index.cfm</link>
			<description></description>
			<language>en-us</language>
			<pubDate>Sun, 05 Sep 2010 13:07:05 -0600</pubDate>
			<lastBuildDate>Fri, 12 Mar 2010 15:04:00 -0600</lastBuildDate>
			<generator>BlogCFC</generator>
			<docs>http://blogs.law.harvard.edu/tech/rss</docs>
			<managingEditor>michael@miuaiga.com</managingEditor>
			<webMaster>michael@miuaiga.com</webMaster>
			<itunes:subtitle></itunes:subtitle>
			<itunes:summary></itunes:summary>
			<itunes:category text="Technology" />
			<itunes:category text="Technology">
				<itunes:category text="Podcasting" />
			</itunes:category>
			<itunes:category text="Technology">
				<itunes:category text="Tech News" />
			</itunes:category>
			<itunes:keywords></itunes:keywords>
			<itunes:author></itunes:author>
			<itunes:owner>
				<itunes:email>michael@miuaiga.com</itunes:email>
				<itunes:name></itunes:name>
			</itunes:owner>
			<itunes:image href="" />
			<image>
				<url></url>
				<title>Making It Up As I Go Along</title>
				<link>http://www.miuaiga.com/index.cfm</link>
			</image>
			<itunes:explicit>no</itunes:explicit>
			
			<item>
				<title>Add a value to a stored list in mySQL using FIND_IN_SET() and CONCAT()</title>
				<link>http://www.miuaiga.com/index.cfm/2010/3/12/Add-a-value-to-a-stored-list-in-mySQL-using-FINDINSET-and-CONCAT</link>
				<description>
				
				For example, a column in a candy store database contains a list of flavors that the different shapes of candy come in. We have another table of flavors, but rather than a third relative table of flavors and product relationships, the IDs of the flavors are simply stored in a column in the products table.

So, you might have a value like
12,14,35,36,78,105
where each number represents the value of a form checkbox in the product admin for this site, and and ID for a flavor in the flavors table. The store admin checks the boxes for the flavors that product comes in, and those IDs get stored as a list in a specific column.

For the purpose of example, suppose the column is simply called &apos;flavors&apos;, and you need to add the flavor &apos;wowzaberry&apos; (id 199) to all of the products that currently have the flavor &apos;razzoberry&apos; (id 105) - with the added logic so that if a candy product already has the new flavor, we don&apos;t add it twice.

&lt;code&gt;
UPDATE tbl_products
SET flavors = CONCAT(flavors,&apos;,199&apos;)
WHERE FIND_IN_SET(&apos;105&apos;,flavors)
AND NOT FIND_IN_SET(&apos;199&apos;,flavors)
&lt;/code&gt;

This says
Add (concat) a comma (,) and 199 (the new ID number)
To any record that already has ID 105 in the flavors column
And that does not already have ID 199 in that same column 
				</description>
				
				<category>mySQL</category>				
				
				<pubDate>Fri, 12 Mar 2010 15:04:00 -0600</pubDate>
				<guid>http://www.miuaiga.com/index.cfm/2010/3/12/Add-a-value-to-a-stored-list-in-mySQL-using-FINDINSET-and-CONCAT</guid>
				
			</item>
			
			<item>
				<title>How to find lowercase (no caps) values in mySQL (and Capitalize them with ColdFusion)</title>
				<link>http://www.miuaiga.com/index.cfm/2010/2/4/How-to-find-lowercase-no-caps-values-in-mySQL-and-Capitalize-them-with-ColdFusion</link>
				<description>
				
				I have a request from a client to help clean up some customer-entered data in a mySQL database, specifically, we need to capitalize all of the values in a column called &apos;user_city&apos;

For the actual capitalization, I am using the very handy CapFirstTitle() function from CFlib : &lt;a href=&quot;http://www.cflib.org/index.cfm?event=page.udfbyid&amp;udfid=116&quot;&gt;http://www.cflib.org/index.cfm?event=page.udfbyid&amp;udfid=116&lt;/a&gt;

But first, I need to find all of the entries that are not already capitalized. This dataset has tens of thousands of records , including some with a blank value in the &apos;city&apos; field - no need to loop all of that just to fix our caps! 

So, here&apos;s the SQL query code to find all of the records with no capital letters at all, by comparing to a lower() all lowercase version of the same value:

&lt;code&gt;
SELECT  user_city, ID 
FROM site_users
WHERE user_city = lower(user_city)
AND NOT user_city = &apos;&apos;
ORDER  by user_city
&lt;/code&gt;

And here is the full CFoutput / CFquery code for the loop and update with Coldfusion, and the CapFirstTitle() function in the head of the page: 

&lt;code&gt;
&lt;cfoutput query=&quot;myQueryAbove&quot;&gt;
	&lt;cfquery datasource=&quot;#application.dsn#&quot;&gt;
	UPDATE site_users
	SET user_city = &apos;#capFirstTitle(myQueryAbove.user_city)#&apos;
	WHERE ID = #myQueryAbove.ID#
	&lt;/cfquery&gt;
&lt;/cfoutput&gt;

&lt;/code&gt;

for other columns you could use OR in the first query and an additional statement in the second, or just change the queries, run the page again, or even, make the column name a variable and just change it once in the head of the page... easy. 
				</description>
				
				<category>mySQL</category>				
				
				<category>ColdFusion</category>				
				
				<pubDate>Thu, 04 Feb 2010 18:30:00 -0600</pubDate>
				<guid>http://www.miuaiga.com/index.cfm/2010/2/4/How-to-find-lowercase-no-caps-values-in-mySQL-and-Capitalize-them-with-ColdFusion</guid>
				
			</item>
			
			<item>
				<title>mySQL str_to_date converts text formatted dates and times to real SQL timestamps</title>
				<link>http://www.miuaiga.com/index.cfm/2010/1/10/mySQL-strtodate-converts-text-formatted-dates-and-times-to-real-SQL-timestamps</link>
				<description>
				
				&lt;p&gt;I am working on a site where dates have been stored as text strings in a mySQL database, such as &apos;2009-10-17&apos; , and I need to create a &apos;search by date&apos; function where a given start and end date can be compared to the date stored in the database. In other words, i need to convert the text-formatted date to a real &apos;datetime&apos; value , that can be compared with (greater than) and (less than) logic within the query itself.&lt;/p&gt;
&lt;p&gt;Like all things CF/mySQL , there are usually multiple ways to get things done. After kicking it around a few different ways, I settled on mySQL&apos;s built-in &quot;str_to_date&quot; function as the easiest and most efficient solution.&lt;/p&gt;
&lt;p&gt;The str_to_date()&amp;nbsp; function takes two arguments: the string that is to be converted, and a format string, which uses the same syntax as mysql date_format() and other related mySQL date functions.&lt;/p&gt;
&lt;p&gt;Since the dates are entered in &apos;yyyy-mm-dd&apos; format, my format string is&quot; %Y-%m-%d &quot;&lt;br /&gt;(Note the capital Y which signifies a 4-digit year)&lt;/p&gt;
&lt;p&gt;My query now looks something like this - note the second line:&lt;/p&gt;
&lt;p&gt;SELECT&lt;br /&gt;str_to_date(aa.date_published,&apos;%Y-%m-%d&apos;) as articleDate,&lt;br /&gt;aa.title, aa.ID,&lt;br /&gt;aa.author,&lt;br /&gt;aa.article&lt;br /&gt;FROM articles_table ...&lt;/p&gt;
&lt;p&gt;This converts the date, 2009-10-17,&lt;br /&gt;to a datetime stamp,&lt;br /&gt;{ts &apos;2009-09-17 00:00:00&apos;}, &lt;br /&gt;which can now be used for any type of date comparisons.&lt;/p&gt;
&lt;p&gt;To keep things clean, if an article happens to be entered with a different format that does not match our format pattern (i.e. somebody types in &apos;10/17/09&apos;),&amp;nbsp; str_to_date() will simply return an empty string in that column, rather than throwing an error.&lt;/p&gt;
&lt;p&gt;The mySQL specification for this and other functions is here: &lt;br /&gt;&lt;a href=&quot;http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date&quot;&gt;http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt; 
				</description>
				
				<category>mySQL</category>				
				
				<category>WebDev</category>				
				
				<pubDate>Sun, 10 Jan 2010 14:00:00 -0600</pubDate>
				<guid>http://www.miuaiga.com/index.cfm/2010/1/10/mySQL-strtodate-converts-text-formatted-dates-and-times-to-real-SQL-timestamps</guid>
				
			</item>
			
			<item>
				<title>How to find Specific Column Names or Types in a mySQL Database</title>
				<link>http://www.miuaiga.com/index.cfm/2009/12/6/How-to-find-Specific-Column-Names-or-Types-in-a-mySQL-Database</link>
				<description>
				
				I&apos;m working on a rather extensive CMS admin application, and need to make some adjustments to all of the &quot;sort&quot; fields in the database. Many of the tables have either a &apos;sort&apos; or &apos;sort_order&apos; (and in one case, I think I saw &apos;SortOrder&apos;), and I need hunt them down, creating a list of column names and table names so i can quickly find the columns I want to edit.

I could open each table one by one, and look for those column names... but of course, there&apos;s a much better way:

&lt;code&gt;
SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE lower(column_name) LIKE &apos;%sort%&apos;
AND TABLE_SCHEMA=&apos;//my database name here//&apos;
&lt;/code&gt;

This returns a perfectly formatted query of table names and column names, along with the &apos;data type&apos; for any column containing &apos;sort&apos; (or &apos;Sort&apos;). Now it will be a snap to make my changes to all of the numeric sort fields in the entire database. 

The query can be modified to return all sorts of useful info, such as all columns in the entire database:

&lt;code&gt;
SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA=&apos;//my database name here//&apos;
&lt;/code&gt;

or all columns with a specific data type

&lt;code&gt;
SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA=&apos;//my database name here//&apos;
AND DATA_TYPE=&apos;varchar&apos;
&lt;/code&gt;

And then, if you wanted to get really lazy, er, creative... you can loop the results of that query, and &apos;do stuff&apos; to all of the columns automatically with just a few more lines:

&lt;code&gt;
alter table #table_Name# modify #column_name# VARCHAR(55) ; 
&lt;/code&gt;

(this would set the size of your varchar field to 55 characters, for example) 
				</description>
				
				<category>mySQL</category>				
				
				<category>WebDev</category>				
				
				<pubDate>Sun, 06 Dec 2009 02:29:00 -0600</pubDate>
				<guid>http://www.miuaiga.com/index.cfm/2009/12/6/How-to-find-Specific-Column-Names-or-Types-in-a-mySQL-Database</guid>
				
			</item>
			
			<item>
				<title>How to find Duplicate Entries in a mySQL Database</title>
				<link>http://www.miuaiga.com/index.cfm/2009/11/25/How-to-find-Duplicate-Entries-in-a-mySQL-Database</link>
				<description>
				
				Here&apos;s a quick easy query that will return all records where a given column&apos;s value appears more than once. 

The trick is the &apos;group by&apos; phrase, combined with the selection of the &apos;count(*)&apos; value, which simply counts the records found for each match of &apos;Name&apos;. 

This query will return any records where the same value appears more than one time in the given column (in this case, the &apos;Name&apos; column), along with the &apos;Counter&apos; value showing how many times the duplicate appears.

&lt;code&gt;
SELECT Name,count(*) as Counter
FROM Table_Name
GROUP BY Name
HAVING Counter &gt; 1
&lt;/code&gt; 
				</description>
				
				<category>mySQL</category>				
				
				<category>WebDev</category>				
				
				<pubDate>Wed, 25 Nov 2009 19:00:00 -0600</pubDate>
				<guid>http://www.miuaiga.com/index.cfm/2009/11/25/How-to-find-Duplicate-Entries-in-a-mySQL-Database</guid>
				
			</item>
			
			<item>
				<title>Get the recordcount of a ColdFusion update, insert or delete query using CFquery result attribute</title>
				<link>http://www.miuaiga.com/index.cfm/2009/8/27/Get-the-recordcount-of-a-ColdFusion-update-insert-or-delete-query-using-CFquery-result-attribute</link>
				<description>
				
				When you run a cfquery using SELECT, the number of records returned is easy to find using #queryname.recordcount# - but what about the other types of SQL queries, our good friends Update, Insert and Delete ? Those query types don&apos;t return a total of rows in the same way.

But there&apos;s an easy solution ... once again, we can use &apos;recordcount&apos;, but in this case it is a node of the &apos;result&apos; variable as defined in our cfquery tag.

&lt;code&gt;

&lt;cfquery datasource=&quot;#request.dsn#&quot; name=&quot;saveChanges&quot; result=&quot;updateResult&quot;&gt;
UPDATE table_name
SET 
column1 = &apos;#value1#&apos;, 
column2 = &apos;#value2#&apos;
WHERE column3 = 1
&lt;/cfquery&gt;

&lt;cfset recordsChanged = updateResult.recordCount&gt;
&lt;/code&gt;

That query is just a sample, you could have any &apos;where&apos; statement, values, etc... but notice the &quot;result&quot; attribute of the cfquery tag, which will accept any variable name you want to use. Then, immediately following the query code, we simply get the &apos;recordcount&apos; node of the result structure, and we have the number of updated records from the query. 

( Note: this example is for mySQL - ymmv with other db types) 
				</description>
				
				<category>mySQL</category>				
				
				<category>ColdFusion</category>				
				
				<pubDate>Thu, 27 Aug 2009 19:06:00 -0600</pubDate>
				<guid>http://www.miuaiga.com/index.cfm/2009/8/27/Get-the-recordcount-of-a-ColdFusion-update-insert-or-delete-query-using-CFquery-result-attribute</guid>
				
			</item>
			
			<item>
				<title>Get the ID of a newly created record from a ColdFusion insert query using CFquery result attribute</title>
				<link>http://www.miuaiga.com/index.cfm/2009/8/27/Get-the-ID-of-a-newly-created-record-from-a-ColdFusion-insert-query-using-CFquery-result-attribute</link>
				<description>
				
				When inserting a new row to a mySQL database table using &amp;lt;cfquery&amp;gt;, it is often necessary to find the ID of the new record so you can refer to it in subsequent queries or other code further down the page.

There are a few ways to do this, but I like this one best:

&lt;code&gt;

&lt;cfquery name=&quot;insertRecord&quot; datasource=&quot;#request.dsn#&quot;  result=&quot;insertResult&quot;&gt;
INSERT INTO my_table
(
column_1,
column_2,
etc
)
VALUES
(
&apos;#value1#&apos;,
&apos;#value2#&apos;,
&apos;etc&apos;
)
&lt;/cfquery&gt;

&lt;cfset newID = insertResult.generated_Key&gt;
&lt;/code&gt;

Notice the &quot;result&quot; attribute of the cfquery tag, which will accept any variable name you want to use. 
Then, immediately following the query code, we simple get the &apos;generated_Key&apos; node of the result value, and voila - we have the ID of the new record, ready for whatever is needed next.

(Note: This example is for mySQL - not sure how this plays out in MS SQL or other DB engines... your mileage may vary.) 
				</description>
				
				<category>mySQL</category>				
				
				<category>ColdFusion</category>				
				
				<pubDate>Thu, 27 Aug 2009 15:48:00 -0600</pubDate>
				<guid>http://www.miuaiga.com/index.cfm/2009/8/27/Get-the-ID-of-a-newly-created-record-from-a-ColdFusion-insert-query-using-CFquery-result-attribute</guid>
				
			</item>
			
			<item>
				<title>Duplicate a mySQL table with one line of SQL code</title>
				<link>http://www.miuaiga.com/index.cfm/2009/5/19/Duplicate-a-mySQL-table-with-one-line-of-SQL-code</link>
				<description>
				
				CREATE TABLE copyname SELECT * FROM originalname

Too easy!

I&apos;m using this to make a backup of an existing table before running a scheduled data import routine (overwriting the backup_table each time by making a new copy of the original) :

&lt;code&gt;

&lt;cfquery datasource=&quot;#request.dsn#&quot; name=&quot;dupTable&quot;&gt;
DROP TABLE backup_table
&lt;/cfquery&gt;
&lt;cfquery datasource=&quot;#request.dsn#&quot; name=&quot;dupTable&quot;&gt;
CREATE TABLE backup_table SELECT * FROM data_table
&lt;/cfquery&gt;

&lt;/code&gt; 
				</description>
				
				<category>mySQL</category>				
				
				<category>WebDev</category>				
				
				<pubDate>Tue, 19 May 2009 19:52:00 -0600</pubDate>
				<guid>http://www.miuaiga.com/index.cfm/2009/5/19/Duplicate-a-mySQL-table-with-one-line-of-SQL-code</guid>
				
			</item>
			
			<item>
				<title>mySQL replace() : find and replace text in existing mySQL data column</title>
				<link>http://www.miuaiga.com/index.cfm/2009/4/25/mySQL-replace--find-and-replace-text-in-existing-mySQL-data-column</link>
				<description>
				
				&lt;p&gt;This is a really useful function - you can find and replace text in any column of a mySQL database using the &quot; replace() &quot; function.&lt;/p&gt;
&lt;p&gt;UPDATE table_name&lt;br /&gt;SET column_name = replace(column_name,&apos;text to find&apos;,&apos;replacement text&apos;);&lt;/p&gt;
&lt;p&gt;easy!&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt; 
				</description>
				
				<category>mySQL</category>				
				
				<category>WebDev</category>				
				
				<pubDate>Sat, 25 Apr 2009 03:34:00 -0600</pubDate>
				<guid>http://www.miuaiga.com/index.cfm/2009/4/25/mySQL-replace--find-and-replace-text-in-existing-mySQL-data-column</guid>
				
			</item>
			
			<item>
				<title>mySQL search query : remove case sensitivity</title>
				<link>http://www.miuaiga.com/index.cfm/2008/12/17/mySQL-search-query--remove-case-sensitivity</link>
				<description>
				
				I&apos;ve had this situation before - a mySQL query using &apos;like&apos; to search a string value for a given phrase sometimes becomes case sensitive when the query contains other &apos;Where&apos; phrases.  ( As far as I know, &apos;like&apos; is supposed to be case INsensitive, and this is a known bug in some installations of mysql - I&apos;ve tried updating mysql drivers and still it remains). But, regardless of the cause, there&apos;s an easy fix...  [More]
				</description>
				
				<category>mySQL</category>				
				
				<category>ColdFusion</category>				
				
				<category>Cartweaver</category>				
				
				<pubDate>Wed, 17 Dec 2008 12:02:00 -0600</pubDate>
				<guid>http://www.miuaiga.com/index.cfm/2008/12/17/mySQL-search-query--remove-case-sensitivity</guid>
				
			</item>
			</channel></rss>