MySQL Tips & Tricks

Sunday, January 24, 2010

mysql insert select example with manipulation of fields

This mysql "insert from another table with select" example shows how you can concatenate two of the selected fields into one inserted field. Obviously you can use any of the other MySQL select string, arithmetic and mathematical operators and functions.

insert into tbl_properties (
agent_property_id,
agent_id,
property_address,
property_borough,
.. etc
)
select
Prop_ID,
concat(prop_firmid,prop_branchid),
Prop_Name,
Prop_Street,
.. etc
from tbl_raw_data;

Labels: ,

Monday, November 24, 2008

PHP Primitive to Create an HTML Form Dropdown from a MySQL table

$options_html=fn_make_form_selection_element($selected);

function fn_make_form_selection_element($selected_value)
{
$select_html="";
$query = "select * from lookup_offices order by office_id";
$result = mysql_query($query) or die ("query not valid".mysql_error());

while($row = mysql_fetch_assoc($result))
{
$db_value=$row['office_id'];
$db_name=$row['office_real_name'];

$option_selected='';
if ($db_value==$selected_value) $option_selected='selected';
$select_html.=<<<_STR_
< option value="$db_value" $option_selected>$db_name< /option>\n
_STR_;
}
return ($select_html);
}

Labels: , , ,