Mysql Query Search - mysql

I need to search columns (shown below) in a table called t_npc , for a specific value i but have failed many times.
example (does not work)
SELECT * FROM t_npc WHERE a_item_0 LIKE '%a_item_%' = 123;
SELECT * FROM t_npc WHERE 'a_item_0' <=> 'a_item_19' = 123;
SELECT a_index FROM t_npc WHERE a_item_0 LIKE 'a_item_%' = 123;
SELECT a_index, a_name FROM t_npc WHERE t_npc.a_item_0 or t_npc.a_item_1 or t_npc.a_item_2 or t_npc.a_item_4 = 44;
and many others to search the column names below for a value but it never works. I have tried wild cards with these columns still with not luck.
`a_item_0` int(11) NOT NULL DEFAULT '-1',
`a_item_1` int(11) NOT NULL DEFAULT '-1',
`a_item_2` int(11) NOT NULL DEFAULT '-1',
`a_item_3` int(11) NOT NULL DEFAULT '-1',
`a_item_4` int(11) NOT NULL DEFAULT '-1',
`a_item_5` int(11) NOT NULL DEFAULT '-1',
`a_item_6` int(11) NOT NULL DEFAULT '-1',
`a_item_7` int(11) NOT NULL DEFAULT '-1',
`a_item_8` int(11) NOT NULL DEFAULT '-1',
`a_item_9` int(11) NOT NULL DEFAULT '-1',
`a_item_10` int(11) NOT NULL DEFAULT '-1',
`a_item_11` int(11) NOT NULL DEFAULT '-1',
`a_item_12` int(11) NOT NULL DEFAULT '-1',
`a_item_13` int(11) NOT NULL DEFAULT '-1',
`a_item_14` int(11) NOT NULL DEFAULT '-1',
`a_item_15` int(11) NOT NULL DEFAULT '-1',
`a_item_16` int(11) NOT NULL DEFAULT '-1',
`a_item_17` int(11) NOT NULL DEFAULT '-1',
`a_item_18` int(11) NOT NULL DEFAULT '-1',
`a_item_19` int(11) NOT NULL DEFAULT '-1',

select value from table where field = 'value'
or
select value from table where field like '%value%'
You are doing both, which will not work. Provide more detail on what you want to accomplish, but from what you asked, that is why your query is not working.
You can, however do:
select value from table where field_a = 'valuea' AND field_b like '%valueb'

You should be using a different table structure.
Try something like this
CREATE TABLE npc (
Npc_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (Npc_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE item (
Npc_id bigint(20) unsigned NOT NULL,
Item_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
Item_value int(11) NOT NULL,
PRIMARY KEY (Item_id),
KEY Npc_id (Npc_id),
CONSTRAINT Item_ibfk_1 FOREIGN KEY (Npc_id) REFERENCES Npc (Npc_id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

The long way:
SELECT a_index, a_name FROM t_npc WHERE
t_npc.a_item_0 = 44 or t_npc.a_item_1 = 44 or t_npc.a_item_2 = 44
...
or t_npc.a_item19 = 44;
Slightly better you can use IN :
select a_index, a_name from t_npc where 44 in
(a_item_0, a_item_1, a_item_2, a_item3,
...
a_item18, a_item19);
You should also create an t_npc_items table instead of having so many fields on t_npc.
[You could generate the sql by querying the field names from the information_schema and then executing that command, but metasql makes unicorns cry.]

Related

Insert into duplicate key and replace into does not work

CREATE TABLE `pv_network_coverage` (
`network_group_id` int(8) NOT NULL,
`carrier_group_id` int(8) NOT NULL,
`physician_count` int(8) NOT NULL DEFAULT '0',
`facility_count` int(8) NOT NULL DEFAULT '0',
`pcp_count` int(8) NOT NULL DEFAULT '0',
`update_time` datetime NOT NULL,
PRIMARY KEY (`network_group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_cs;
REPLACE INTO pv_network_coverage(carrier_group_id,
network_group_id,
physician_count,
facility_count,
pcp_count,
update_time) SELECT *FROM temp_pv_network_coverage;
I use replace into and insert into with duplicate keys.
The physician_count should be 1000 but still 0..
It really makes me surprised.
Remember to select columns instead of select *from

mysql query taking about 3 seconds - and all keys indexed

I have a simple join between 3 mysql tables with 1613, 369, and 500 rows respectively. I believe I have everything indexed but the query is still taking 3 seconds or more. My query is:
SELECT
fe.pid, fe.encounter, s.Templates_ID
FROM
(form_encounter fe
JOIN em__structure s ON fe.pc_catid=s.Templates_ID
JOIN em__assemblies a ON s.Massemblies_ID=a.Massemblies_ID)
When I do an EXPLAIN on this I get the following, sorry for the format:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE a index Massemblies_ID Massemblies_ID 4 NULL 651 Using index; Using temporary; Using filesort
1 SIMPLE s ref Templates_ID,Massemblies_ID Massemblies_ID 4 oaks_1410.a.Massemblies_ID 1
1 SIMPLE fe ref pc_catid pc_catid,4,oaks_1410.s.Templates_ID 100 Using where
Here are my 3 tables, I am trying to put every available clue in place to figure this out:
CREATE TABLE `form_encounter` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`date` datetime DEFAULT NULL,
`reason` longtext,
`facility` longtext,
`facility_id` int(11) NOT NULL DEFAULT '0',
`pid` bigint(20) DEFAULT NULL,
`encounter` bigint(20) DEFAULT NULL,
`onset_date` datetime DEFAULT NULL,
`sensitivity` varchar(30) DEFAULT NULL,
`pc_catid` int(11) NOT NULL DEFAULT '5' COMMENT 'event category from openemr_postcalendar_categories',
`last_stmt_date` date DEFAULT NULL,
`stmt_count` int(11) NOT NULL DEFAULT '0',
`provider_id` int(11) DEFAULT '0' COMMENT 'default and main provider for this visit',
`supervisor_id` int(11) DEFAULT '0' COMMENT 'supervising provider, if any, for this visit',
`invoice_refno` varchar(31) NOT NULL DEFAULT '',
`referral_source` varchar(31) NOT NULL DEFAULT '',
`billing_facility` int(11) NOT NULL DEFAULT '0',
`billing_status` int(11) DEFAULT '10' COMMENT 'see form_encounter_status_codes',
`billing_due_date` date DEFAULT NULL COMMENT 'date an encounter becomes due',
`last_level_billed` int(11) NOT NULL DEFAULT '0' COMMENT '0=none, 1=ins1, 2=ins2, etc',
`last_level_closed` int(11) NOT NULL DEFAULT '0' COMMENT '0=none, 1=ins1, 2=ins2, etc',
`billing_note` text,
`workmans_comp` varchar(1) DEFAULT 'N',
`workmans_comp_claim_number` varchar(40) DEFAULT NULL COMMENT 'workmans comp claim number',
`prior_authorization_number` varchar(40) DEFAULT NULL COMMENT 'prior authorization number for workmans comp',
`auto_accident` varchar(1) DEFAULT 'N',
`auto_accident_state` varchar(2) DEFAULT NULL COMMENT 'auto accident state',
`other_accident` varchar(1) DEFAULT 'N',
`referral_provider_id` int(11) DEFAULT NULL,
`referral_provider_npi` varchar(10) DEFAULT NULL,
`signed_by` int(11) NOT NULL DEFAULT '0' COMMENT 'Provider who signed the encounter',
`pc_templateid` varchar(10) DEFAULT NULL COMMENT 'Physical exam template used for this encounter',
`pc_poptemplateid` int(10) unsigned DEFAULT NULL COMMENT 'Encounter template including data population',
`pc_poptemplatestatus` tinyint(1) unsigned DEFAULT NULL COMMENT 'Whether ecounter has been materially modified from template',
`deleted` tinyint(4) NOT NULL DEFAULT '0' COMMENT 'flag indicates form has been deleted',
PRIMARY KEY (`id`),
UNIQUE KEY `pid_encounter_unique` (`pid`,`encounter`) USING BTREE,
KEY `pc_catid` (`pc_catid`)
) ENGINE=InnoDB AUTO_INCREMENT=1614 DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC
Second table:
CREATE TABLE `em__structure` (
`ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
`Templates_ID` int(11) unsigned DEFAULT NULL,
`Sections_ID` int(11) unsigned DEFAULT NULL,
`Idx` mediumint(4) unsigned DEFAULT '1',
`Massemblies_ID` int(5) unsigned NOT NULL DEFAULT '0',
`EditDate` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`),
KEY `Templates_ID` (`Templates_ID`),
KEY `Sections_ID` (`Sections_ID`),
KEY `Massemblies_ID` (`Massemblies_ID`)
) ENGINE=InnoDB
Third table:
CREATE TABLE `em__assemblies` (
`ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
`Massemblies_ID` int(5) unsigned NOT NULL DEFAULT '0',
`Groupings_ID` int(11) unsigned DEFAULT NULL,
`Components_ID` int(11) unsigned DEFAULT NULL,
`Idx` mediumint(4) unsigned DEFAULT '1',
`Sex` char(1) DEFAULT NULL,
`Responsetypes_ID` int(11) DEFAULT NULL,
PRIMARY KEY (`ID`),
KEY `Groupings_ID` (`Groupings_ID`),
KEY `Components_ID` (`Components_ID`),
KEY `Responsetypes_ID` (`Responsetypes_ID`),
KEY `Massemblies_ID` (`Massemblies_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=505 DEFAULT CHARSET=latin1
The joins are all on indexed integer keys.

Mysql query with special characters?

I (newbie) have hard time to understand this query:
$result = mysql_query("
SELECT q.*, IF(v.id,1,0) AS voted
FROM quotes AS q
LEFT JOIN quotes_votes AS v
ON q.id = v.qid
AND v.ip =".$ip."
AND v.date_submit = '".$today."'
");
can anybody provide more info on what these short symbols like 'q.*' with the if statement and v.id,1,0. Any sources to read more about this?
Thank you very much.
this is how the tables looks like:
CREATE TABLE `quotes` (
`id` smallint(5) unsigned NOT NULL auto_increment,
`txt` varchar(255) collate utf8_unicode_ci NOT NULL default '',
`author` varchar(32) collate utf8_unicode_ci NOT NULL default '',
`bgc` varchar(32) collate utf8_unicode_ci NOT NULL default '',
`votes` mediumint(9) unsigned NOT NULL default '0',
`vsum` int(11) unsigned NOT NULL default '0',
`rating` double NOT NULL default '0',
PRIMARY KEY (`id`),
KEY `rating` (`rating`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=16 ;
CREATE TABLE `quotes_votes` (
`id` mediumint(9) unsigned NOT NULL auto_increment,
`qid` smallint(6) unsigned NOT NULL default '0',
`ip` int(10) NOT NULL default '0',
`vote` tinyint(1) NOT NULL default '0',
`date_submit` date NOT NULL default '0000-00-00',
`dt_submit` timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `qid` (`qid`,`ip`,`date_submit`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Regarding select q.*, it just means getting all fields from the table alias q, which happens to be table quotes. It is like select * but just for one table.
Regarding IF(v.id,1,0), that is really a MySQLism. The IF statement evaluates an expression given in the first argument and, if it is true, returns the second argument. Otherwise it returns the third argument. So you know that a 1 or a 0 will come out of the IF. You might now be wondering how can v.id be evaluated to return a logical value and the reason behind it is that MySQL treats booleans as if they were TINYINT(1) in which 0 is considered as false and non-zero values are considered as true.
So that would be rephrased into IF(v.id != 0, 1, 0) which might be easier to read. Given the fact that v.id can not be null then you could rewrite that this way IF(v.id = 0, 0, 1). Anyway, you could take a step further and just replace it with v.id != 0 :)

Find row that have a duplicate field, the filed type is blob

I have a table with many many duplicated row, I cannot create a unique value for the blob field, because is too large.
How can I find and delete the duplicate rows where the blob field (answer) is duplicated?
This is the table structure :
CREATE TABLE `answers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_question` int(11) NOT NULL,
`id_user` int(11) NOT NULL,
`answer` blob NOT NULL,
`language` varchar(2) NOT NULL,
`datetime` datetime NOT NULL,
`enabled` int(11) NOT NULL DEFAULT '0',
`deleted` int(11) NOT NULL DEFAULT '0',
`spam` int(11) NOT NULL DEFAULT '0',
`correct` int(11) NOT NULL DEFAULT '0',
`notification_send` int(11) NOT NULL DEFAULT '0',
`correct_notification` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `id_question` (`id_question`),
KEY `id_user` (`id_user`),
KEY `enabled` (`enabled`)
) ENGINE=InnoDB AUTO_INCREMENT=1488 DEFAULT CHARSET=utf8mb4
probable you can use prefix of the column by substr() or left() and compare. How much size you want to take that depends on your data distribution or prefix uniqueness of the column data.
for uniqueness check you can fire the below query if the
select count(distinct left(answer, 128))/count(*), count(distinct left(answer, 256))/count(*) from answers.
This will provide you selectivity or data distribution in your column. suppose 128 gives you answer as 1 i.e. all unique if you take first 128 bytes then choose that amount of data from each row and work. Hope it helps.

MySQL Results pivot table - of a sort

I have a table as defined below:
CREATE TABLE `z_data` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`subscriberid` INT(11) NOT NULL DEFAULT '0',
`Email Address` VARCHAR(255) NOT NULL DEFAULT '',
`Title` VARCHAR(255) NOT NULL DEFAULT '',
`First Name` VARCHAR(255) NOT NULL DEFAULT '',
`Last Name` VARCHAR(255) NOT NULL DEFAULT '',
`Postal Code` VARCHAR(255) NOT NULL DEFAULT '',
`banned` INT(11) NOT NULL DEFAULT '0',
`bounced` INT(1) NOT NULL DEFAULT '0',
`unsub` INT(1) NOT NULL DEFAULT '0',
`duplicate` INT(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `idx_subscriberid` (`subscriberid`),
KEY `idx_banned` (`banned`),
KEY `idx_bounced` (`bounced`),
KEY `idx_unsub` (`unsub`),
KEY `idx_duplicate` (`duplicate`),
KEY `idx_email` (`Email Address`),
FULLTEXT KEY `idx_emailaddress` (`Email Address`)
) ENGINE=MYISAM AUTO_INCREMENT=20 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC
This table is populated from a CSV file and a query updates the subscriberid column with a number if the emailaddress is allready present in the main database with a query like:
UPDATE `z_data` AS z
LEFT JOIN main_subscribers AS b ON (z.`Email Address`=b.emailaddress && b.listid=1 && b.unsubscribed!=0)
SET z.subscriberid = b.subscriberid
WHERE b.subscriberid IS NOT NULL;";
Now, my question. I need to select the subscriberids of all of the records in z_data in rows of ten fields, this can be comma seperated lists.
my result could be similar to:
1293572,1293573,1293574,1293575,1099590,1174275,1293576,1293577,1293578,1293579,
673070,813617,1293580,1293581,1293582,1131221,1293583,1182045,419085,1293584,
1050278,1293585,1064945,638483,737691,1293586,1293587,799800,1110596,1293588,
1293589,1293590,1293591,421394,1293592,1293593,1293594,1293595,1293596,851491,
1293597,1293598,1293599,628250,1293600,1293601,1293602,535366,1293603,256590,
1293604,1293605,736956,1293606,1209511,673075,1293607,1293608,1293609,754357,
The reason for this is so that I can store these values in a TEXT field for later use in a IN clause and yet have a reasonably human readable script.
I have managed to get the first row thus:
SELECT GROUP_CONCAT(a.subscriberid) AS 'IDs' FROM (
SELECT subscriberid FROM
`z_data`
WHERE subscriberid!=0
LIMIT 1,10
) AS a
but do not know how to 'walk' though the rest of the table when the number of rows with subscriberids is unknown.
Any suggestions would be gratefully received.