Using slash in MySQL database - mysql

I am using mysql with php Yii framework.When doing data modelling I came accross some doubt.I have a table for user details like this.
CREATE TABLE IF NOT EXISTS `tbl_users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`firstname` varchar(80) NOT NULL DEFAULT '',
`lastname` varchar(80) NOT NULL DEFAULT '',
`gender` varchar(6) NOT NULL DEFAULT '',
`email` varchar(45) NOT NULL DEFAULT '',
`company_name` varchar(80) NOT NULL DEFAULT '',
`contact_no` varchar(45) NOT NULL DEFAULT '',
`address` varchar(120) NOT NULL DEFAULT '',
`state` varchar(45) NOT NULL DEFAULT '',
`country` varchar(45) NOT NULL DEFAULT '',
`created_by` int(11) DEFAULT NULL,
`updated_by` int(11) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=98 ;
Here my problem is I want the state,country should be like this so that I can use the fields like state/city
`state/city` varchar(45) NOT NULL DEFAULT '',
`state/province` varchar(45) NOT NULL DEFAULT '',
So is it safe to use like this or I have to use only one option there?Any help and suggestions will be highly appriciable.

Yes, it's valid. But you will always need to quote it with backticks.
See http://dev.mysql.com/doc/refman/5.5/en/identifiers.html for full details on what's allowed.

Related

Is there a better way to get this than multiple sub-queries, perhaps with more joins?

I have several tables joined together with the purpose of determining the "best", meaning the smallest, box that a single item or a group of these items will fit in to.
Its just to get me "close enough" so I can then determine the shipping cost of that item or items by different carriers.
I have combined the two steps of 1 getting each item and its measurements via MySQl query and 2 getting the "best" box by querying the results from the first query, with one mySql query that does them both for me.
The issue now is that its too slow, I would like this query to run much faster, I have looked at optimizing indexes, but that doesn't seem to help. I think perhaps there's a better way to structure the query to get a faster result set.
SELECT
Listings.PriceCodeDetail.RecNbr AS PCDRecNbr,
(SELECT RecNbr FROM Boxes
WHERE
(
GREATEST(LENGTH, Width, Height) >= GREATEST(atr_grail_live.ip_Spec.ItemLength,atr_grail_live.ip_Spec.ItemWidth,atr_grail_live.ip_Spec.ItemHeight) #GreatestMeasurement
AND LEAST(LENGTH, Width, Height) >= LEAST(atr_grail_live.ip_Spec.ItemLength,atr_grail_live.ip_Spec.ItemWidth,atr_grail_live.ip_Spec.ItemHeight) #LeastMeasurement
AND (LENGTH + Width + Height) - LEAST(LENGTH, Width, Height) - GREATEST(LENGTH, Width, Height) >= (atr_grail_live.ip_Spec.ItemLength+atr_grail_live.ip_Spec.ItemWidth+atr_grail_live.ip_Spec.ItemHeight) - LEAST(atr_grail_live.ip_Spec.ItemLength,atr_grail_live.ip_Spec.ItemWidth,atr_grail_live.ip_Spec.ItemHeight) - GREATEST(atr_grail_live.ip_Spec.ItemLength,atr_grail_live.ip_Spec.ItemWidth,atr_grail_live.ip_Spec.ItemHeight) #MedianMeasurement
)
AND WEIGHT >= (Listings.PriceCodeDetail.QtyBreak * (Listings.ItemListingDetail.ListingQty * atr_grail_live.ip_Spec.ItemWeight)) #TotalWeight
ORDER BY CuIn
LIMIT 1) AS IdealBox # This finds the Longest side, shortest side and middle side and compares it to the Longest, shortest and middle of the item(s) then makes sure the weight is greater that the item(s) weight(s)
FROM
Listings.ItemListingHeader
INNER JOIN Listings.ItemListingDetail ON Listings.ItemListingDetail.HeaderRecNbr = Listings.ItemListingHeader.RecNbr
INNER JOIN Listings.PriceCodeHeader ON Listings.PriceCodeHeader.ListingRecNbr = Listings.ItemListingHeader.RecNbr
INNER JOIN Listings.PriceCodeDetail ON Listings.PriceCodeDetail.HeaderRecNbr = Listings.PriceCodeHeader.RecNbr
INNER JOIN atr_grail_live.ip_Spec ON Listings.ItemListingDetail.IPRecNbr = atr_grail_live.ip_Spec.IP_RecNbr
WHERE
Listings.ItemListingHeader.MarketplaceRecNbr = 1 AND
Listings.PriceCodeHeader.CustomerPriceLevelRecNbr IN (4,5)
AND Listings.ItemListingHeader.RecNbr NOT IN (
SELECT
Listings.ItemListingHeader.RecNbr
FROM
Listings.ItemListingHeader
INNER JOIN Listings.ItemListingDetail ON Listings.ItemListingDetail.HeaderRecNbr = Listings.ItemListingHeader.RecNbr
INNER JOIN Listings.PriceCodeHeader ON Listings.PriceCodeHeader.ListingRecNbr = Listings.ItemListingHeader.RecNbr
INNER JOIN Listings.PriceCodeDetail ON Listings.PriceCodeDetail.HeaderRecNbr = Listings.PriceCodeHeader.RecNbr
INNER JOIN atr_grail_live.ip_Spec ON Listings.ItemListingDetail.IPRecNbr = atr_grail_live.ip_Spec.IP_RecNbr
WHERE
Listings.ItemListingHeader.MarketplaceRecNbr = 1 AND
Listings.PriceCodeHeader.CustomerPriceLevelRecNbr IN (4,5)
AND (atr_grail_live.ip_Spec.ItemLength IS NULL OR atr_grail_live.ip_Spec.ItemLength = '')
GROUP BY Listings.ItemListingHeader.RecNbr
) # This removes from the result set any item(s) that don't have measurements and aren't part of specific groups I have defined, PriceLevel and Marketplace in this instance.
AND atr_grail_live.ip_Spec.IP_RecNbr IN (47467))
# The last AND is only there for now to limit it to one group of items... it will not be used once this is optimized.
I have left some comments in there so as to better explain what I am doing. Currently this query takes about 4 seconds and returns 14 rows.
If I remove the last line that limits it to one item it would be using over 250K item combinations...so would take a LOOOONG time.
Note that each subquery runs very quickly itself, so I think I have indexes correct.
Also note that if this can't be optimized like this, I could change the structure of any of the tables to accommodate. I was thinking I could store Boxes and Items in Length,Width,Height and actually force Length to be longest, Width to be in the Middle and Height to be the shortest side. Would that help?
Thanks for any pointers on this.
*** Added more comments ****
If it helps the reason I am doing all that math with the greatest and least is to determine the Longest Side, Shortest Side, and then the one in the middle (Median). So since I have 3 values L,W,H (Length,Width,Height) and can determine the largest and smallest values in SQL, the one left over after subtracting the other two is the Median.
* Added Table layouts *
CREATE TABLE `ItemListingHeader` (
`RecNbr` int(11) NOT NULL AUTO_INCREMENT,
`SKU` varchar(255) NOT NULL,
`MarketplaceRecNbr` int(11) NOT NULL,
`MarketplaceListingID` varchar(255) NOT NULL,
`MarketplaceShippingTemplateRecNbr` int(11) DEFAULT NULL,
`Status` varchar(1) DEFAULT NULL,
`QtyAvailToReport` int(11) DEFAULT NULL,
`QtyUpdated` datetime DEFAULT NULL,
PRIMARY KEY (`RecNbr`),
UNIQUE KEY `UniqueKey` (`SKU`,`MarketplaceRecNbr`,`MarketplaceListingID`) USING BTREE,
UNIQUE KEY `UniqueKey2` (`SKU`,`MarketplaceRecNbr`) USING BTREE,
KEY `RecNbr` (`RecNbr`)
) ENGINE=InnoDB AUTO_INCREMENT=36351 DEFAULT CHARSET=utf8
CREATE TABLE `ItemListingDetail` (
`RecNbr` int(11) NOT NULL AUTO_INCREMENT,
`HeaderRecNbr` int(11) NOT NULL,
`IPRecNbr` int(11) NOT NULL,
`ListingQty` int(11) DEFAULT NULL,
PRIMARY KEY (`RecNbr`,`HeaderRecNbr`,`IPRecNbr`),
UNIQUE KEY `UniqueKey` (`HeaderRecNbr`,`IPRecNbr`) USING BTREE,
CONSTRAINT `HeaderKey` FOREIGN KEY (`HeaderRecNbr`) REFERENCES `ItemListingHeader` (`RecNbr`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=36344 DEFAULT CHARSET=utf8
CREATE TABLE `PriceCodeHeader` (
`RecNbr` int(11) NOT NULL AUTO_INCREMENT,
`ListingRecNbr` int(11) NOT NULL,
`CustomerPriceLevelRecNbr` int(11) NOT NULL,
`CustomerNbr` int(11) NOT NULL,
PRIMARY KEY (`RecNbr`,`ListingRecNbr`,`CustomerPriceLevelRecNbr`,`CustomerNbr`),
UNIQUE KEY `UniqueKey1` (`ListingRecNbr`,`CustomerPriceLevelRecNbr`) USING BTREE,
KEY `RecNbr` (`RecNbr`),
CONSTRAINT `ListingHeader` FOREIGN KEY (`ListingRecNbr`) REFERENCES `ItemListingHeader` (`RecNbr`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=85976 DEFAULT CHARSET=utf8mb4
CREATE TABLE `PriceCodeDetail` (
`RecNbr` int(11) NOT NULL AUTO_INCREMENT,
`HeaderRecNbr` int(11) NOT NULL,
`PricingMethod` varchar(255) DEFAULT NULL,
`QtyBreak` int(11) NOT NULL,
`Floor` double(11,2) DEFAULT NULL,
`Ceiling` double(11,2) DEFAULT NULL,
`Modifier` double(11,2) DEFAULT NULL,
`Override` double(11,2) DEFAULT NULL,
`AmznMod` double(11,2) DEFAULT NULL,
`PackagingSqFt` double(11,2) DEFAULT NULL,
`LaborMinutes` double(11,2) DEFAULT NULL,
`OtherCosts` double(11,2) DEFAULT NULL,
`FloorMultiplier` double(11,2) DEFAULT NULL,
`CeilingMultiplier` double(11,2) DEFAULT NULL,
`DiscountMultiplier` double(11,2) DEFAULT NULL,
`CurrPrice` double(11,2) DEFAULT NULL,
PRIMARY KEY (`RecNbr`),
UNIQUE KEY `UniqueKey` (`HeaderRecNbr`,`QtyBreak`) USING BTREE,
CONSTRAINT `Header` FOREIGN KEY (`HeaderRecNbr`) REFERENCES `PriceCodeHeader` (`RecNbr`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=242526 DEFAULT CHARSET=utf8mb4
CREATE TABLE `ip_Spec` (
`IP_RecNbr` int(11) NOT NULL DEFAULT '0',
`Amperage` varchar(255) DEFAULT '',
`ANSICode` varchar(255) DEFAULT '',
`Base` varchar(255) DEFAULT '',
`BallastCode` varchar(255) DEFAULT '',
`BeamSpread` varchar(255) DEFAULT '',
`BurnPosition` varchar(255) DEFAULT '',
`Candlepower` varchar(255) DEFAULT '',
`ColorTemp` varchar(255) DEFAULT '',
`ColorTemp_Filter` varchar(255) DEFAULT '',
`Color` varchar(255) DEFAULT '',
`Color_Filter` varchar(255) DEFAULT '',
`CRI` varchar(255) DEFAULT '',
`Diameter` varchar(255) DEFAULT '',
`Diameter_Filter` varchar(255) DEFAULT '',
`DocumentFile1` varchar(255) DEFAULT '',
`DocumentFile2` varchar(255) DEFAULT '',
`DocumentFile3` varchar(255) DEFAULT '',
`DocumentFile4` varchar(255) DEFAULT '',
`DocumentFile5` varchar(255) DEFAULT '',
`DocumentDescription1` varchar(255) DEFAULT '',
`DocumentDescription2` varchar(255) DEFAULT '',
`DocumentDescription3` varchar(255) DEFAULT '',
`DocumentDescription4` varchar(255) DEFAULT '',
`DocumentDescription5` varchar(255) DEFAULT '',
`DocumentType1` varchar(255) DEFAULT '',
`DocumentType2` varchar(255) DEFAULT '',
`DocumentType3` varchar(255) DEFAULT '',
`DocumentType4` varchar(255) DEFAULT '',
`DocumentType5` varchar(255) DEFAULT '',
`Filament` varchar(255) DEFAULT '',
`Finish` varchar(255) DEFAULT '',
`GlassSize` varchar(255) DEFAULT '',
`GlassSize_Filter` varchar(255) DEFAULT '',
`HourLife` varchar(255) DEFAULT '',
`InitialLumens` varchar(255) DEFAULT '',
`ImageFile` varchar(255) DEFAULT '',
`AddtlImage1` varchar(255) DEFAULT '',
`AddtlImage2` varchar(255) DEFAULT '',
`AddtlImage3` varchar(255) DEFAULT '',
`AddtlImage4` varchar(255) DEFAULT '',
`AddtlImage5` varchar(255) DEFAULT '',
`LCL` varchar(255) DEFAULT '',
`Length` varchar(255) DEFAULT '',
`Length_Filter` varchar(255) DEFAULT '',
`Lumens` varchar(255) DEFAULT '',
`Voltage` varchar(255) DEFAULT '',
`Voltage_Filter` varchar(255) DEFAULT '',
`Wattage` varchar(255) DEFAULT '',
`Wattage_Filter` varchar(255) DEFAULT '',
`ShipCode` varchar(255) DEFAULT '',
`SpecStatus` varchar(11) DEFAULT NULL,
`Country_of_Origin` varchar(255) DEFAULT '',
`Contents` varchar(255) DEFAULT '',
`HS_Code` varchar(255) DEFAULT '',
`Dimmable` varchar(1) DEFAULT 'N',
`Enclosure_Rated` varchar(1) DEFAULT 'N',
`Rough_Service` varchar(1) DEFAULT 'N',
`Self_Ballasted` varchar(1) DEFAULT 'N',
`Rapid_Start` varchar(1) DEFAULT 'N',
`Pulse_Start` varchar(1) DEFAULT 'N',
`Covered_Glass` varchar(1) DEFAULT 'N',
`Energy_Star` varchar(1) DEFAULT 'N',
`ROHOS` varchar(1) DEFAULT 'N',
`Description` varchar(255) DEFAULT '',
`BallastType` varchar(255) DEFAULT '',
`BallastStartMethod` varchar(255) DEFAULT '',
`NumberOfLamps` varchar(11) DEFAULT NULL,
`BallastFactor` varchar(11) DEFAULT NULL,
`BallastProductTechnology` varchar(255) DEFAULT NULL,
`MinimumStartTemperature` varchar(11) DEFAULT NULL,
`TotalHarmonicDistortion` varchar(11) DEFAULT NULL,
`EmergencyBallast` varchar(1) DEFAULT 'N',
`CurrentType` varchar(255) DEFAULT '',
`OutputCurrent` varchar(11) DEFAULT NULL,
`OutputCurrentUnitOfMeasure` varchar(255) DEFAULT '',
`OutputVoltage` varchar(11) DEFAULT NULL,
`OutputVoltageUnitOfMeasure` varchar(255) DEFAULT '',
`PowerFactor` varchar(11) DEFAULT NULL,
`Efficiency` varchar(11) DEFAULT NULL,
`Programmable` varchar(1) DEFAULT 'N',
`MinimumWattage` varchar(11) DEFAULT NULL,
`MaximumWattage` varchar(11) DEFAULT NULL,
`HousingMaterial` varchar(255) DEFAULT '',
`LenseMaterial` varchar(255) DEFAULT '',
`MountingStyle` varchar(255) DEFAULT '',
`LightSourceType` varchar(255) DEFAULT '',
`ReflectorType` varchar(255) DEFAULT '',
`IntegratedLightSource` varchar(1) DEFAULT 'N',
`PhotoCellIncluded` varchar(1) DEFAULT 'N',
`SpecsComplete` varchar(1) DEFAULT 'N',
`LocationRating` varchar(255) DEFAULT '',
`ItemLength` varchar(11) DEFAULT NULL,
`ItemLengthUnitOfMeasure` varchar(255) DEFAULT '',
`ItemWidth` varchar(11) DEFAULT NULL,
`ItemWidthUnitOfMeasure` varchar(255) DEFAULT '',
`ItemHeight` varchar(11) DEFAULT NULL,
`ItemHeightUnitOfMeasure` varchar(255) DEFAULT '',
`ItemWeight` varchar(11) DEFAULT NULL,
`ItemWeightUnitOfMeasure` varchar(255) DEFAULT '',
`ProductFamily` varchar(255) DEFAULT '',
`ShortDescription` varchar(255) DEFAULT '',
`LongDescription` text,
`InternalNotes` tinytext,
`BulletPoint1` varchar(255) DEFAULT '',
`BulletPoint2` varchar(255) DEFAULT '',
`BulletPoint3` varchar(255) DEFAULT '',
`BulletPoint4` varchar(255) DEFAULT '',
`BulletPoint5` varchar(255) DEFAULT '',
`WarrantyYears` varchar(11) DEFAULT NULL,
`ETL` varchar(1) DEFAULT 'N',
`CE` varchar(1) DEFAULT 'N',
`UL` varchar(1) DEFAULT 'N',
`DLC` varchar(1) DEFAULT 'N',
`TCLP` varchar(1) DEFAULT 'N',
`IPRated` varchar(1) DEFAULT 'N',
`IPRating` varchar(11) DEFAULT NULL,
`Standby` varchar(1) DEFAULT 'N',
`CeramicMetalHalide` varchar(1) DEFAULT 'N',
`UVProtected` varchar(1) DEFAULT 'N',
`LIFCode` varchar(255) DEFAULT '',
`EnergySaver` varchar(1) DEFAULT 'N',
`ElectricalRequirements` varchar(255) DEFAULT '',
`LumensPerWatt` varchar(11) DEFAULT NULL,
`Disclaimer` text,
`InputElectricalPolarity` varchar(255) DEFAULT '',
`Atmosphere` varchar(255) DEFAULT '',
`BaseMaterial` varchar(255) DEFAULT '',
`GlassMaterial` varchar(255) DEFAULT '',
`DimmablePercentage` varchar(11) DEFAULT NULL,
`ServiceType` varchar(255) DEFAULT '',
`GlassShape` varchar(255) DEFAULT '',
`OutputElectricalPolarity` varchar(255) DEFAULT NULL,
`BeamSpreadDesc` varchar(255) DEFAULT NULL,
`Case1Description` varchar(255) DEFAULT NULL,
`Case1Qty` varchar(255) DEFAULT NULL,
`Case1Length` varchar(255) DEFAULT NULL,
`Case1Width` varchar(255) DEFAULT NULL,
`Case1Height` varchar(255) DEFAULT NULL,
`Case1Weight` varchar(255) DEFAULT NULL,
`Case1GTIN` varchar(255) DEFAULT NULL,
`Case1EAN` varchar(255) DEFAULT NULL,
`Case2Description` varchar(255) DEFAULT NULL,
`Case2Qty` varchar(255) DEFAULT NULL,
`Case2Length` varchar(255) DEFAULT NULL,
`Case2Width` varchar(255) DEFAULT NULL,
`Case2Height` varchar(255) DEFAULT NULL,
`Case2Weight` varchar(255) DEFAULT NULL,
`Case2GTIN` varchar(255) DEFAULT NULL,
`Case2EAN` varchar(255) DEFAULT NULL,
`Case3Description` varchar(255) DEFAULT NULL,
`Case3Qty` varchar(255) DEFAULT NULL,
`Case3Length` varchar(255) DEFAULT NULL,
`Case3Width` varchar(255) DEFAULT NULL,
`Case3Height` varchar(255) DEFAULT NULL,
`Case3Weight` varchar(255) DEFAULT NULL,
`Case3GTIN` varchar(255) DEFAULT NULL,
`Case3EAN` varchar(255) DEFAULT NULL,
`Case4Description` varchar(255) DEFAULT NULL,
`Case4Qty` varchar(255) DEFAULT NULL,
`Case4Length` varchar(255) DEFAULT NULL,
`Case4Width` varchar(255) DEFAULT NULL,
`Case4Height` varchar(255) DEFAULT NULL,
`Case4Weight` varchar(255) DEFAULT NULL,
`Case4GTIN` varchar(255) DEFAULT NULL,
`Case4EAN` varchar(255) DEFAULT NULL,
PRIMARY KEY (`IP_RecNbr`),
UNIQUE KEY `IpRec` (`IP_RecNbr`) USING BTREE,
KEY `SpecStatus` (`SpecStatus`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=ascii
CREATE TABLE `Boxes` (
`RecNbr` int(10) NOT NULL AUTO_INCREMENT,
`Length` int(10) NOT NULL,
`Width` int(10) NOT NULL,
`Height` int(10) NOT NULL,
`Weight` decimal(10,1) NOT NULL,
`CuIn` int(10) NOT NULL,
PRIMARY KEY (`RecNbr`)
) ENGINE=InnoDB AUTO_INCREMENT=184656 DEFAULT CHARSET=utf8mb4
Your NOT IN where statement seems unnecessary. I suggest replacing it with
atr_grail_live.ip_Spec.ItemLength IS NOT NULL
AND atr_grail_live.ip_Spec.ItemLength <> ''
in the main query.
This should provide the same results, but without the need for the NOT IN subquery, as that subquery is basically just a duplicate of your main query.
The only big savings I see after that is
Listings.PriceCodeHeader.CustomerPriceLevelRecNbr IN (4,5)
Any kind of IN or OR is generally a good place to try to speed things up, as exact matches tend to be handled much faster.
Splitting your query up into two queries, one with
Listings.PriceCodeHeader.CustomerPriceLevelRecNbr = 4
and the other with
Listings.PriceCodeHeader.CustomerPriceLevelRecNbr = 5
and then using UNION ALL to combine the query results may also provide some improvement.
After that, only a missed index somewhere is likely. Provide an EXPLAIN plan of this optimized query and I may be able to help determine where another index could be useful.
That said, you are really on top of your game when it comes to indexing. Most people looking for query optimization help don't have any concept of how indexes work, but you are an exception.
WHERE Listings.ItemListingHeader.MarketplaceRecNbr = 1
AND Listings.PriceCodeHeader.CustomerPriceLevelRecNbr IN (4, 5)
AND Listings.ItemListingHeader.RecNbr NOT IN ( SELECT ... )
Change the NOT IN to a LEFT JOIN ... IS NULL
The WHERE clause is referencing multiple tables, there is no way to add an index that spans multiple tables. Anyway, have an index on MarketplaceRecNbr. (More in a minute.) And on CustomerPriceLevelRecNbr.
You have several inefficiencies in your indexes. Study these rules:
A PRIMARY KEY is a UNIQUE key is an INDEX. (cf `ItemListingHeader.RecNbr)
If you have a unique key whose columns is a subset of another unique key, the latter is automatically unique and should be changed to INDEX to avoid extra effort.
INDEX(a,b) is unnecessary if you also have the same beginning columns: INDEX(a,b,c). Your case is more complex since it involves a uniqueness constraint. UNIQUE(a,b), UNIQUE(a,b,c) --> UNIQUE(a,b), INDEX(b,a,c)
INDEX(a,b) takes care of any need for INDEX(a), but it does not take care of INDEX(b). That is the ordering matters.
From those rules, make a stab at a better set of indexes for each table. I'll critique it.

Need to optimize mysql query of a PHP framework

I have a query which is generated via PHP framework core function. I don't have control to change the query. Therefore i will need to perform optimization on server side i.e. mysql to execute this query in efficient time. I have applied some indices but still it is taking around 4-5 seconds and ideally it should take 1-1.5 seconds. Following is the query:
(
SELECT rr.rt_bids_aos_quotes_relaos_quotes_idb AS so_id,
rr.sales_order_sequence sequence,
so.*
FROM rt_bids_aos_quotes_rel AS rr
INNER JOIN aos_quotes AS so ON so.id = rr.rt_bids_aos_quotes_relaos_quotes_idb
WHERE rr.deleted = 0
AND rr.rt_bids_aos_quotes_relrt_bids_ida='490395-403600-b'
)
UNION
(
SELECT ra.rt_bids_aos_quotes_altaos_quotes_idb AS so_id,
'' AS sequence,
so.*
FROM rt_bids_aos_quotes_alternate AS ra
INNER JOIN aos_quotes AS so ON so.id = ra.rt_bids_aos_quotes_altaos_quotes_idb
WHERE ra.deleted = 0
AND ra.rt_bids_aos_quotes_altrt_bids_ida='490395-403600-b'
)
Following is the Explain query result:Image_here
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY rt_bids_aos_quotes_rel const idx_rt_bids_aos_quotes_relrt_bids_ida_deleted,rt_bids_aos_quotes_rel_alt,idx_rt_bids_aos_quotes_rel_rt_bids_aos_quotes_relaos_quotes_idb idx_rt_bids_aos_quotes_relrt_bids_ida_deleted 113 const,const 1 NULL
1 PRIMARY so ALL NULL NULL NULL NULL 631950 Using where
2 UNION NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
NULL UNION RESULT <union1,2> ALL NULL NULL NULL NULL NULL Using temporary
Show create table results :
CREATE TABLE `rt_bids_aos_quotes_rel` (
`id` varchar(36) NOT NULL,
`date_modified` datetime DEFAULT NULL,
`deleted` tinyint(1) DEFAULT '0',
`rt_bids_aos_quotes_relrt_bids_ida` varchar(36) DEFAULT NULL,
`rt_bids_aos_quotes_relaos_quotes_idb` varchar(36) DEFAULT NULL,
`sales_order_sequence` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_rt_bids_aos_quotes_relrt_bids_ida_deleted` (`deleted`,`rt_bids_aos_quotes_relrt_bids_ida`),
KEY `rt_bids_aos_quotes_rel_alt` (`rt_bids_aos_quotes_relrt_bids_ida`,`rt_bids_aos_quotes_relaos_quotes_idb`),
KEY `idx_rt_bids_aos_quotes_rel_rt_bids_aos_quotes_relaos_quotes_idb` (`rt_bids_aos_quotes_relaos_quotes_idb`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `aos_quotes` (
`id` char(36) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`date_entered` datetime DEFAULT NULL,
`date_modified` datetime DEFAULT NULL,
`modified_user_id` char(36) DEFAULT NULL,
`created_by` char(36) DEFAULT NULL,
`description` text,
`deleted` tinyint(1) DEFAULT '0',
`assigned_user_id` char(36) DEFAULT NULL,
`approval_issue` text,
`billing_account_id` char(36) DEFAULT NULL,
`billing_contact_id` char(36) DEFAULT NULL,
`billing_address_street` varchar(150) DEFAULT NULL,
`billing_address_city` varchar(100) DEFAULT NULL,
`billing_address_state` varchar(100) DEFAULT NULL,
`billing_address_postalcode` varchar(20) DEFAULT NULL,
`billing_address_country` varchar(255) DEFAULT NULL,
`shipping_address_street` varchar(150) DEFAULT NULL,
`shipping_address_city` varchar(100) DEFAULT NULL,
`shipping_address_state` varchar(100) DEFAULT NULL,
`shipping_address_postalcode` varchar(20) DEFAULT NULL,
`shipping_address_country` varchar(255) DEFAULT NULL,
`expiration` date DEFAULT NULL,
`number` int(11) NOT NULL,
`opportunity_id` char(36) DEFAULT NULL,
`template_ddown_c` text,
`total_amt` decimal(26,6) DEFAULT NULL,
`total_amt_usdollar` decimal(26,6) DEFAULT NULL,
`subtotal_amount` decimal(26,6) DEFAULT NULL,
`subtotal_amount_usdollar` decimal(26,6) DEFAULT NULL,
`discount_amount` decimal(26,6) DEFAULT NULL,
`discount_amount_usdollar` decimal(26,6) DEFAULT NULL,
`tax_amount` decimal(26,6) DEFAULT NULL,
`tax_amount_usdollar` decimal(26,6) DEFAULT NULL,
`shipping_amount` decimal(26,6) DEFAULT NULL,
`shipping_amount_usdollar` decimal(26,6) DEFAULT NULL,
`shipping_tax` varchar(100) DEFAULT NULL,
`shipping_tax_amt` decimal(26,6) DEFAULT NULL,
`shipping_tax_amt_usdollar` decimal(26,6) DEFAULT NULL,
`total_amount` decimal(26,6) DEFAULT NULL,
`total_amount_usdollar` decimal(26,6) DEFAULT NULL,
`currency_id` char(36) DEFAULT NULL,
`stage` varchar(100) DEFAULT 'Draft',
`term` varchar(100) DEFAULT NULL,
`terms_c` text,
`approval_status` varchar(100) DEFAULT NULL,
`invoice_status` varchar(100) DEFAULT 'Not Invoiced',
`subtotal_tax_amount` decimal(26,6) DEFAULT NULL,
`subtotal_tax_amount_usdollar` decimal(26,6) DEFAULT NULL,
`bid_id` char(36) DEFAULT NULL,
`alt` varchar(255) DEFAULT NULL,
`group_desc` longtext,
`hold` tinyint(1) DEFAULT '0',
`order_amount` decimal(26,2) DEFAULT NULL,
`order_description` longtext,
`status` varchar(100) DEFAULT NULL,
`type` varchar(255) DEFAULT NULL,
`contract_id` char(36) DEFAULT NULL,
`customer_discount` decimal(26,2) DEFAULT NULL,
`customer_markup` decimal(26,2) DEFAULT NULL,
`markup_inv_type` decimal(26,2) DEFAULT NULL,
`location_id` char(36) DEFAULT NULL,
`active` varchar(100) DEFAULT 'Active',
`city` varchar(255) DEFAULT NULL,
`rt_jobs_id` char(36) DEFAULT NULL,
`system_type` varchar(36) DEFAULT NULL,
`com_address` varchar(255) DEFAULT NULL,
`com_city` varchar(255) DEFAULT NULL,
`com_mapsco` varchar(255) DEFAULT NULL,
`com_state_zip` varchar(255) DEFAULT NULL,
`job_type` varchar(100) DEFAULT NULL,
`calc_labor` varchar(100) DEFAULT 'Item_Install_amt',
`comission` decimal(10,4) DEFAULT '0.0000',
`hourly_labor_rate` decimal(8,4) DEFAULT NULL,
`labor_percentage_of_price` decimal(12,2) DEFAULT NULL,
`overhead` decimal(12,4) DEFAULT '0.0000',
`profit` decimal(12,4) DEFAULT '0.0000',
`pricing_checkbox` tinyint(1) DEFAULT '0',
`pkg_package_id` char(36) DEFAULT NULL,
`dh_factor` decimal(6,2) DEFAULT '0.00',
`addl_builder_price` decimal(12,4) DEFAULT '0.0000',
`billing_notes` longtext,
`billing_notes_home` longtext,
`builder_percentage` decimal(12,4) DEFAULT '0.0000',
`discount` decimal(12,4) DEFAULT '0.0000',
`jobs_contact_homeowner_id` char(36) DEFAULT NULL,
`mortage` varchar(100) DEFAULT 'mortage',
`oh_percentage` decimal(12,2) DEFAULT '0.00',
`origin` varchar(255) DEFAULT NULL,
`package_items` decimal(10,2) DEFAULT NULL,
`package_items_unit_left` decimal(10,2) DEFAULT '0.00',
`selected_package_units` decimal(10,2) DEFAULT '0.00',
`jobs_account_superintendent_id` char(36) DEFAULT NULL,
`subdivision_selected_id` char(36) DEFAULT NULL,
`subdivision_selected_name` varchar(255) DEFAULT NULL,
`department` varchar(255) DEFAULT 'Dallas',
`locked` tinyint(1) DEFAULT '0',
`billed_amount` decimal(26,2) DEFAULT '0.00',
`billed_percentage` decimal(26,2) DEFAULT '0.00',
`retained_amount` decimal(26,2) DEFAULT '0.00',
`selected_package_amount` decimal(26,2) DEFAULT '0.00',
`builder_amount` decimal(26,2) DEFAULT '0.00',
`home_owner_amount` decimal(26,2) DEFAULT '0.00',
`builderContractAmount` decimal(26,2) DEFAULT '0.00',
`homeOwnerContractAmount` decimal(26,2) DEFAULT '0.00',
`ho_pct` decimal(26,6) DEFAULT '0.000000',
`builder_pct` decimal(26,6) DEFAULT '0.000000',
`labor_pct` decimal(26,6) DEFAULT '0.000000',
`mortgage` tinyint(1) DEFAULT '0',
`ho_order` tinyint(1) DEFAULT '0',
`home_owner_sale_order` tinyint(1) DEFAULT '0',
`directly_created_contract` tinyint(1) DEFAULT '0',
`crew_manager_id` char(36) DEFAULT NULL,
`estimate_date` date DEFAULT NULL,
`complete` tinyint(1) DEFAULT '0',
`complete_note` text,
`plan_estimate_date` date DEFAULT NULL,
`plan_manager_id` char(36) DEFAULT NULL,
`lock_bid_id` char(36) DEFAULT NULL,
`documents_id` text,
`no_item_change` tinyint(1) DEFAULT '0',
`tagged_at_yard` tinyint(1) DEFAULT '0',
`soap_created_so` tinyint(1) DEFAULT '0',
`soap_created_so_amount` decimal(10,2) DEFAULT '0.00',
`subcon_vendor` varchar(255) DEFAULT NULL,
`plan_complete` varchar(100) DEFAULT 'In Progress',
`plan_required` tinyint(1) DEFAULT '0',
`plan_important` varchar(255) DEFAULT '0',
`confirm` varchar(100) DEFAULT 'Unconfirm',
`designer_notes` text,
`plan_due_date` date DEFAULT NULL,
`plan_required_so` tinyint(1) DEFAULT '0',
`plan_request` tinyint(1) DEFAULT '0',
`material_hold_status` varchar(255) DEFAULT NULL,
`wh_warehouse_id` char(36) DEFAULT NULL,
`work_order_created` tinyint(1) DEFAULT '0',
`maintenance_status` varchar(100) DEFAULT NULL,
`classification_type` varchar(255) DEFAULT NULL,
`pricing_type` varchar(255) DEFAULT NULL,
`estimate_end_date` date DEFAULT NULL,
`install_date` date DEFAULT NULL,
`is_matched` tinyint(1) DEFAULT '0',
`builder_discount` decimal(10,2) DEFAULT '0.00',
`ho_discount` decimal(10,2) DEFAULT '0.00',
`locate_required` tinyint(1) DEFAULT '0',
`priority` varchar(100) DEFAULT NULL,
`is_no_charged` tinyint(1) DEFAULT '0',
`no_charge_reasons` varchar(255) DEFAULT NULL,
`no_charge_users` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_aos_quotes_type` (`type`),
KEY `idx_aos_quotes_rt_jobs_id` (`rt_jobs_id`),
KEY `idx_aos_quotes_id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `rt_bids_aos_quotes_alternate` (
`id` varchar(36) NOT NULL,
`date_modified` datetime DEFAULT NULL,
`deleted` tinyint(1) DEFAULT '0',
`rt_bids_aos_quotes_altrt_bids_ida` varchar(36) DEFAULT NULL,
`rt_bids_aos_quotes_altaos_quotes_idb` varchar(36) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_rt_bids_aos_quotes_altrt_bids_ida_deleted` (`deleted`,`rt_bids_aos_quotes_altrt_bids_ida`),
KEY `rt_bids_aos_quotes_alt` (`rt_bids_aos_quotes_altrt_bids_ida`,`rt_bids_aos_quotes_altaos_quotes_idb`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Please advise how can i improve it.
Your default charset for aos_quotes is latin1, but the default charset for the other tables is utf8. When you JOIN based on comparing two strings with different collations, they can't use the index, so they are forced to do a table-scan. I have no doubt that's what's slowing down your query.
When I use your tables as is, I get this EXPLAIN for the so tables:
*************************** 2. row ***************************
id: 1
select_type: PRIMARY
table: so
partitions: NULL
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 1
filtered: 100.00
Extra: Using where
When I convert your aos_quotes table to utf8, I get this EXPLAIN for the so tables:
*************************** 2. row ***************************
id: 1
select_type: PRIMARY
table: so
partitions: NULL
type: const
possible_keys: PRIMARY,idx_aos_quotes_id
key: PRIMARY
key_len: 108
ref: const
rows: 1
filtered: 100.00
Extra: NULL
The "type: PRIMARY" is much better than "type: ALL".
So you need to convert your aos_quotes table to utf8.
See How do I change a MySQL table to UTF-8?
Other possible improvements:
A PRIMARY KEY is a UNIQUE key is a KEY. So KEY idx_aos_quotes_id (id) is redundant and could (should) be dropped.
I see VARCHAR(36); that usually smacks of random UUIDs. But your query shows something else. What is the situation. (The randomness of standard UUIDs is a performance problem in large tables.)
I see lots of TEXT columns and SELECT .. so.* fetching all of them. If you don't need them all, don't fetch them all. Each one is potentially an extra disk hit.
UNION defaults to UNION DISTINCT, which involves a de-dup pass. If you don't need that, then UNION ALL would be faster.
(Not a speed issue, just a readability issue.) Can you remove the clutter of the "rt_bids_aos_quotes_" prefix?
If rt_bids_aos_quotes_alternate and rt_bids_aos_quotes_rel are many:many mappings, see my blog for several performance tips.
decimal(26,6) takes 12 bytes. Do you need that much precision and range? Or would some other datatype do? Smaller -> more cacheable -> less I/O -> faster. (Rule of Thumb: DECIMAL(m,n) occupies about m/2 bytes. INT and FLOAT occupy exactly 4 bytes. TINYINT: 1 byte.)
In addition to fixing the CHARSET for id, fix the inconsistency between CHAR(36) and VARCHAR(36), especially if your ids are only 15 characters. (Again, space may lead to speed.)
I see UNIQUE(deleted, ida) -- Does this mean that there can be 1 or 2 rows with a given ida? If there can be only one (either deleted or not), then make (ida) the PK.

correct mysql syntax error

please could someone tell me the problem with this syntax because mysql 5.5.32 keeps tell me about an error
CREATE TABLE `clients` (
`ID` tinyint(11) NOT NULL auto_increment,
`title` varchar(10) NOT NULL default '',
`firstName` varchar(30) NOT NULL default '',
`lastName` varchar(30) NOT NULL default '',
`address1` varchar(100) NOT NULL default '',
`address2` varchar(100) NOT NULL default '',
`town` varchar(100) NOT NULL default '',
`province` varchar(100) NOT NULL default '',
`country` varchar(40) NOT NULL default '',
`postCode` varchar(20) NOT NULL default '',
`telephone` varchar(20) NOT NULL default '',
`email` varchar(100) NOT NULL default '',
`cardNo` varchar(16) NOT NULL default '0000-00-00',
`expiryDate` date NOT NULL default '0000-00-00',
PRIMARY KEY (`ID`)
) TYPE=MyISAM COMMENT='customer table' AUTO_INCREMENT=1 ;
The keyword TYPE has been replaced by ENGINE as in
ENGINE=MyISAM
change TYPE to ENGINE like this:
CREATE TABLE `clients` (
`ID` tinyint(11) NOT NULL auto_increment,
`title` varchar(10) NOT NULL default '',
`firstName` varchar(30) NOT NULL default '',
`lastName` varchar(30) NOT NULL default '',
`address1` varchar(100) NOT NULL default '',
`address2` varchar(100) NOT NULL default '',
`town` varchar(100) NOT NULL default '',
`province` varchar(100) NOT NULL default '',
`country` varchar(40) NOT NULL default '',
`postCode` varchar(20) NOT NULL default '',
`telephone` varchar(20) NOT NULL default '',
`email` varchar(100) NOT NULL default '',
`cardNo` varchar(16) NOT NULL default '0000-00-00',
`expiryDate` date NOT NULL default '0000-00-00',
PRIMARY KEY (`ID`)
) ENGINE=MyISAM COMMENT='customer table' AUTO_INCREMENT=1 ;
MySQL 5.0 accepts TYPE or ENGINE, but above MySQL 5.1, Only ENGINE is allowed.

need help Mysql query

I am trying to achieve displaying a field in my database.
basically at the moment i have
'SELECT
historylist.artist,
historylist.ID,
artistlist.lyrics,
artistlist.ID
FROM historylist
INNER JOIN artistlist
ON historylist.ID = artistlist.ID
ORDER BY historylist.date_played DESC
LIMIT 1;'
Now this is not correct. I need to use the history list ID to link the artistlist ID. Then grab the field artistlist.lyrics. then display it. Right now when i do it like that it shows the lyrics field but its null. So i am guessing its searching historylist table
CREATE TABLE `historylist` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`songID` int(11) NOT NULL DEFAULT '0',
`filename` varchar(255) NOT NULL DEFAULT '',
`date_played` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`duration` int(11) NOT NULL DEFAULT '0',
`artist` varchar(255) NOT NULL DEFAULT '',
`title` varchar(255) NOT NULL DEFAULT '',
`album` varchar(255) NOT NULL DEFAULT '',
`albumyear` varchar(4) NOT NULL DEFAULT '',
`website` varchar(255) NOT NULL DEFAULT '',
`buycd` varchar(255) NOT NULL DEFAULT '',
`picture` varchar(255) NOT NULL DEFAULT '',
`listeners` mediumint(9) NOT NULL DEFAULT '0',
`label` varchar(100) NOT NULL DEFAULT '',
`pline` varchar(50) NOT NULL DEFAULT '',
`trackno` int(11) NOT NULL DEFAULT '0',
`composer` varchar(100) NOT NULL DEFAULT '',
`ISRC` varchar(50) NOT NULL DEFAULT '',
`catalog` varchar(50) NOT NULL DEFAULT '',
`UPC` varchar(50) NOT NULL DEFAULT '',
`feeagency` varchar(20) NOT NULL DEFAULT '',
PRIMARY KEY (`ID`),
KEY `date_played` (`date_played`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
that code above is the historylist table
this one is the
CREATE TABLE IF NOT EXISTS `artistlist` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`filename` varchar(255) NOT NULL DEFAULT '',
`diskID` int(11) NOT NULL DEFAULT '0',
`flags` varchar(10) NOT NULL DEFAULT 'NNNNNNNNNN',
`songtype` char(1) NOT NULL DEFAULT 'S',
`status` tinyint(4) NOT NULL DEFAULT '0',
`weight` double NOT NULL DEFAULT '50',
`balance` double NOT NULL DEFAULT '0',
`date_added` datetime DEFAULT NULL,
`date_played` datetime DEFAULT NULL,
`date_artist_played` datetime DEFAULT '2002-01-01 00:00:01',
`date_album_played` datetime DEFAULT '2002-01-01 00:00:01',
`date_title_played` datetime DEFAULT '2002-01-01 00:00:01',
`duration` int(11) NOT NULL DEFAULT '0',
`artist` varchar(255) NOT NULL DEFAULT '',
`title` varchar(255) NOT NULL DEFAULT '',
`album` varchar(255) NOT NULL DEFAULT '',
`label` varchar(255) NOT NULL DEFAULT '',
`pline` varchar(50) NOT NULL DEFAULT '',
`trackno` int(11) NOT NULL DEFAULT '0',
`composer` varchar(100) NOT NULL DEFAULT '',
`ISRC` varchar(50) NOT NULL DEFAULT '',
`catalog` varchar(50) NOT NULL DEFAULT '',
`UPC` varchar(50) NOT NULL DEFAULT '',
`feeagency` varchar(20) NOT NULL DEFAULT '',
`albumyear` varchar(4) NOT NULL DEFAULT '0',
`genre` varchar(20) NOT NULL DEFAULT '',
`website` varchar(255) NOT NULL DEFAULT '',
`buycd` varchar(255) NOT NULL DEFAULT '',
`info` text,
`lyrics` text,
`picture` varchar(255) NOT NULL DEFAULT '',
`count_played` mediumint(9) NOT NULL DEFAULT '0',
`count_requested` mediumint(9) NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`),
UNIQUE KEY `filename` (`filename`),
KEY `date_played` (`date_played`),
KEY `date_artist_played` (`date_artist_played`),
KEY `date_album_played` (`date_album_played`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=347 ;
Now basically on every song i can show the data making the call to history list. but i need to use the historylist id its pulling. to then connect to the artistlist id so it pulls the lyrics field
hope that helps more
Seems like your join condition is incorrect.
ON historylist.ID = artistlist.ID
If this to be work both table have to have 1-1 relationship. Looking at your table structure i think correct condition would be
ON historylist.artist = artistlist.artist

bugs in mysql when connecting 2 table

When I am trying to rum query I find error like
#1267 - Illegal mix of collations (latin1_general_ci,IMPLICIT) and (latin1_swedish_ci,IMPLICIT) for operation '='
My table structure are
CREATE TABLE `empregistration` (
`id` int(100) NOT NULL auto_increment,
`cname` text NOT NULL,
`cpersonname` text NOT NULL,
`roaddress` text NOT NULL,
`txtcity` text NOT NULL,
`txtstate` text NOT NULL,
`txtcountry` text NOT NULL,
`faxno` varchar(255) NOT NULL,
`contactno` varchar(255) NOT NULL,
`url` varchar(255) NOT NULL,
`empCat` varchar(255) NOT NULL,
`empno` varchar(255) NOT NULL,
`ctype` varchar(255) NOT NULL,
`establishyear` varchar(255) NOT NULL,
`txtjobcategory` text NOT NULL,
`cemailid` varchar(255) NOT NULL,
`calteremailid` varchar(255) NOT NULL,
`aboutcompany` text NOT NULL,
`password` varchar(255) NOT NULL,
`conpassword` varchar(255) NOT NULL,
`empLogo` varchar(255) NOT NULL,
`paymenttype` varchar(255) NOT NULL,
`regDate` int(11) default NULL,
`countViewProf` varchar(200) default '0',
`cntDownProf` varchar(200) default '0',
`cntJobPost` varchar(200) default '0',
`cntupdatejob` varchar(200) NOT NULL default '0',
`limitjobpost` varchar(200) NOT NULL default '0',
`limitofupdatejob` varchar(200) NOT NULL default '0',
`limitofviewcv` varchar(200) NOT NULL default '0',
`limitofdowncv` varchar(200) NOT NULL default '0',
`paymentforpostjob` varchar(200) NOT NULL default '0',
`paymentforsearchcv` varchar(200) NOT NULL default '0',
`takenPlan` varchar(255) default '0',
`planDate` date default NULL,
`takenplanforpostjob` varchar(200) NOT NULL default '0',
`postjobplandate` varchar(200) NOT NULL default '0',
`cntJobPost1` varchar(200) default '0',
`cntupdatejob1` varchar(200) default '0',
`status` varchar(255) NOT NULL default 'Active',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=214 ;
and other table is
CREATE TABLE `blockedcompanies` (
`blockedId` int(11) NOT NULL auto_increment,
`empId` varchar(50) collate latin1_general_ci NOT NULL,
`regId` int(11) NOT NULL,
`jobSeekId` varchar(50) collate latin1_general_ci NOT NULL,
`blockDate` date NOT NULL,
PRIMARY KEY (`blockedId`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=5 ;
You don't show your query, but i expect you're doing a where somehow with an '=' in there. Now your default collation seems to be swedish, and your 'blockedcompanies' table has a collation to latin1_general.
The error says "implicit", so you're not doing something with collation in your query. Now you have to different collation being compared. You've got to change that using COLLATE
check this: http://dev.mysql.com/doc/refman/5.0/en/charset-collate.html
when you create the table empregistration, change the final line from
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=214 ;
to
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=214 ;
and your query should start working.