MySQL MATCH AGAINST JOIN issue - php

I'm making a PHP + MySQL app for company records management and I need an joint result from several tables based on fulltext search in every table.
The query is formed dynamically and in worst case looks so:
SELECT `contracts_info`.`contract_number`, `contracts_info`.`balance`, `contracts_info`.`conclusion_date`, `contracts_info`.`activation_date`, `contracts_info`.`deactivation_date`,
`parents`.`mother_fullname`, `parents`.`mother_email`, `parents`.`mother_phone`, `parents`.`father_fullname`, `parents`.`father_email`, `parents`.`father_phone`, `parents`.`postal_office`,
`students`.`name`, `students`.`second_name`, `students`.`surname`, `students`.`form_number`, `students`.`form_letter`,
`students_info`.`medical_features`, `students_info`.`psychological_features`
FROM contracts_info
JOIN parents USING(contract_number)
JOIN students USING(contract_number)
JOIN students_info USING(contract_number)
WHERE MATCH (`contracts_info`.`contract_number`) AGAINST (? IN BOOLEAN MODE)
OR MATCH (`parents`.`contract_number`, `parents`.`mother_fullname`, `parents`.`mother_email`, `parents`.`father_fullname`, `parents`.`father_email`) AGAINST (? IN BOOLEAN MODE)
OR MATCH (`students`.`name`, `students`.`surname`, `students`.`contract_number`) AGAINST (? IN BOOLEAN MODE)
OR MATCH (`students_info`.`medical_features`, `students_info`.`psychological_features`, `students_info`.`contract_number`) AGAINST (? IN BOOLEAN MODE);
The problem is that when the query above is executed, I get an error from PDO class
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'contracts_info.contract_number' in 'where clause'
I've trying running a query with names enclosed in backquotes or not, this doesn't make any difference.
Queries without JOINS work well. Also I investigated these questions
mySQL MATCH across multiple tables
MySQL Optional LEFT JOIN With MATCH
but they didn't seem to have alike problem.
I don't understand why this happens because I give the exact name of columns and don't use AS aliases.
So why MySQL fails in finding and existing column from existing table?
MySQL version(): 5.6.29-76.2, PHP 5.5.9
Thank you in advance.
EDIT:
Table definitions:
CREATE TABLE IF NOT EXISTS `contracts_info` (
`contract_number` varchar(11) COLLATE utf8_unicode_ci NOT NULL,
`balance` decimal(7,2) unsigned zerofill NOT NULL,
`conclusion_date` date NOT NULL,
`activation_date` date NOT NULL,
`deactivation_date` date NOT NULL,
PRIMARY KEY (`contract_number`),
FULLTEXT KEY `contract_number` (`contract_number`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS `students` (
`contract_number` varchar(11) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
`second_name` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
`surname` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
`form_number` enum('1','2','3','4','5','6','7','8','9','10','11','12') COLLATE utf8_unicode_ci NOT NULL,
`form_letter` enum('А','Б','В','Г','Д') COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`contract_number`),
FULLTEXT KEY `name` (`name`),
FULLTEXT KEY `surname` (`surname`),
FULLTEXT KEY `contract_number` (`contract_number`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS `students_info` (
`contract_number` varchar(11) COLLATE utf8_unicode_ci NOT NULL,
`medical_features` text COLLATE utf8_unicode_ci NOT NULL,
`psychological_features` text COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`contract_number`),
FULLTEXT KEY `medical_features` (`medical_features`),
FULLTEXT KEY `psycological_features` (`psychological_features`),
FULLTEXT KEY `contract_number` (`contract_number`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS `parents` (
`contract_number` varchar(11) COLLATE utf8_unicode_ci NOT NULL,
`mother_fullname` varchar(90) COLLATE utf8_unicode_ci NOT NULL,
`mother_email` varchar(45) COLLATE utf8_unicode_ci NOT NULL,
`mother_phone` decimal(15,0) NOT NULL,
`father_fullname` varchar(90) COLLATE utf8_unicode_ci NOT NULL,
`father_email` varchar(45) COLLATE utf8_unicode_ci NOT NULL,
`father_phone` decimal(15,0) NOT NULL,
`postal_office` int(4) NOT NULL,
PRIMARY KEY (`contract_number`),
FULLTEXT KEY `contract_number` (`contract_number`),
FULLTEXT KEY `mother_fullname` (`mother_fullname`),
FULLTEXT KEY `mother_email` (`mother_email`),
FULLTEXT KEY `father_fullname` (`father_fullname`),
FULLTEXT KEY `father_email` (`father_email`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Related

Unique value when INSERT INTO mysql

I have the following table in mysql:
CREATE TABLE `kampbs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user` varchar(20) COLLATE utf8_danish_ci DEFAULT NULL,
`k1` varchar(1) COLLATE utf8_danish_ci DEFAULT NULL,
`k1r` varchar(10) COLLATE utf8_danish_ci DEFAULT NULL,
`k2` varchar(1) COLLATE utf8_danish_ci DEFAULT NULL,
`k2r` varchar(10) COLLATE utf8_danish_ci DEFAULT NULL,
`k3` varchar(1) COLLATE utf8_danish_ci DEFAULT NULL,
`k3r` varchar(10) COLLATE utf8_danish_ci DEFAULT NULL,
`week` int(2) DEFAULT NULL,
`grp` varchar(11) COLLATE utf8_danish_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_danish_ci
I have a form, that INSERT INTO the fields in the database, but it should only be possible to insert once a week pr user. So it should check if the column 'user' and 'week' are unique together. Make sense? :/
Let's say the user 'freak' submit in week 13, he shouldn't be able to submit once again in week 13.
I use the following $sql:
$sql = "INSERT INTO kampbs(user, k1, k1r, k2, k2r, k3, k3r, week, grp) VALUES('$username', '$k1', '$k1r', '$k2', '$k2r', '$k3', '$k3r', '$week', '$grp')";
You can add a constraint to your table, so that the user / week couple is forced to be unique
ALTER TABLE `kampbs` ADD UNIQUE `unique_index`(`user`, `week`);

Result consisted of more than one row SQL=INSERT INTO

While inserting first time it gives following error: "Result consisted of more than one row". When i try to insert record second time it gives error with message duplicate entry.
SQL=INSERT INTO `master_user` (`name`,`user_name`,`email`,`password`,`system_name_of_friend`,`system_no_of_friend`,`registered_from_site`,`registered_on`,`is_existing_user`) VALUES ('FirstName LastName','username','demo#mail.com','8c71eede42e38709e9e836021b0b9b9b','','','site','','1')
any one help will be appropriated and following is the table structure which will be get help to tracking this issue very easily and get the solution for this.
CREATE TABLE IF NOT EXISTS `master_user` (
`master_user_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`user_name` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`system_name_of_friend` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`system_no_of_friend` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`registered_from_ip` varchar(25) COLLATE utf8_unicode_ci DEFAULT NULL,
`registered_from_site` varchar(25) COLLATE utf8_unicode_ci DEFAULT NULL,
`registered_on` datetime DEFAULT NULL,
`is_existing_user` bit(1) NOT NULL DEFAULT b'0',
PRIMARY KEY (`master_user_id`),
UNIQUE KEY `ukMasterUser_email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1293 ;
I have changed the few words from column Which are conflicting and resolved.

Is it possible to set 2 separate unique fields using Redbean ORM

I am using the redbeanPHP ORM and mysql. I have the following table:
CREATE TABLE `mast` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`note` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`geolocation` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`location` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`zip` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`state` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`app` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UQ_84a93b55f688c94f73092dba1b9590c41a92cbf5` ('app')
) ENGINE=InnoDB AUTO_INCREMENT=98 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
I want to insert records into the 'mast' table providing they are unique with respect to both of the 2 fields listed above. In other words if either 'geolocation' or 'app' is a duplicate, I don't want to insert the associated record.
I am using following php code to create the 2 unique fields:
$mast= R::dispense('mast');
$mast->setMeta("buildcommand.unique" , array(array('geolocation')));
$mast ->import($resultsarray);
$mast->setMeta("buildcommand.unique" , array(array('app')));
$id = R::store($mast); // DUMMY BEAN
A unique index is only being created for the 'app' field . Is there a way to set them both as unique in redbean?
From the documentation it should work like the following:
$mast = R::dispense('mast');
$mast->setMeta("buildcommand.unique.0", array('geolocation', 'app'));
$mast->import($resultsarray);
$id = R::store($mast);
In your code you just overwrote the value for build command.unique.

Cascade on delete sql [duplicate]

This question already has answers here:
MySQL foreign key constraints, cascade delete
(3 answers)
Closed 8 years ago.
Im working on a small website that involves the use of databases, I'm using this two tables to users, in the first table i add the users and his information, and in the second one I add only the user id the course id.
My question is how can I add the cascade on delete, in which table?
CREATE TABLE IF NOT EXISTS `students` (
`studentid` int(11) NOT NULL AUTO_INCREMENT,
`fname` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`lname` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`studentcode` varchar(16) COLLATE utf8_unicode_ci NOT NULL,
`courseid` int(11) NOT NULL,
PRIMARY KEY (`studentid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=27 ;
CREATE TABLE IF NOT EXISTS `idtables` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`courseid` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`studentid` char(64) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=43 ;
You need to define this behaviour in a FOREIGN KEY constraint like:
CREATE TABLE IF NOT EXISTS `idtables` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`courseid` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`studentid` char(64) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY fk_students_id (studentid)
REFERENCES students (id)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=43 ;
You need to create a FORIEGN KEY referencing to students table studentid with ON DELETE CASCADE option like below
CREATE TABLE IF NOT EXISTS `idtables` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`courseid` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`studentid` char(64) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT FK1 FORIEGN KEY(studentid) REFERENCES students(studentid) <-- Here
ON DELETE CASCADE <-- HERE
)

Find posts of user grouped by month, unixtime

I'd like to find the number of posts for each user grouped by month.
I'm currently using INT(10) unsigned to store the date of posts.
what would be a super fast way to do this?
CREATE TABLE IF NOT EXISTS `media` (
`pid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`class` tinyint(1) NOT NULL DEFAULT '1',
`date_class_changed` int(10) unsigned NOT NULL,
`title` char(5) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,
`url` varchar(1024) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`media` enum('image','video') CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`thumb` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`description` varchar(140) COLLATE utf8_unicode_ci NOT NULL,
`username` varchar(16) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`date` int(10) unsigned NOT NULL,
`file` varchar(1024) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`hash` char(32) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`hashtag` text CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`meta` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`ip` int(10) unsigned NOT NULL,
`kind` tinyint(1) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`pid`),
UNIQUE KEY `title` (`title`),
KEY `hash` (`hash`),
KEY `class_date` (`class`,`date_class_changed`),
KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1022724 ;
This is the table, I'm talking about, I'd like to display the number of posts for each user for each mont, such as: september 2012, User X, N posts etc..
The query I'm using after the help from #fthiella is:
SELECT
DATE_FORMAT(FROM_UNIXTIME(`date`), '%Y-%m') as YearMonth, username, COUNT(*) as Posts
FROM
media
WHERE username = 'foobar'
GROUP BY 1
ORDER BY 1 DESC
thanks God it's fast enough, now I'll try to optimize in case it's not using an index, but for now with almost 1M record is doing good. cheers.
SELECT
DATE_FORMAT(FROM_UNIXTIME(`date`), '%Y-%m') as YearMonth,
username,
COUNT(*) as Posts
FROM
media
GROUP BY
DATE_FORMAT(FROM_UNIXTIME(`date`), '%Y-%m') as YearMonth,
username

Categories