why I'm getting this?
Error: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'favorites' in field list is ambiguous
SQL Query: UPDATE `twitter`.`tractions` AS `Traction` LEFT JOIN `twitter`.`profiles` AS `Profile` ON (`Traction`.`profile_id` = `Profile`.`id`) SET `Traction`.`favorites` = `favorites` + 1, `Traction`.`errors` = `errors` + 0 WHERE `Traction`.`profile_id` = 4 AND `Traction`.`date` = '2013-01-11'
CakePHP code:
$this->Traction->updateAll(array(
"Traction.favorites"=>"`favorites` + $favorites",
"Traction.errors"=>"`errors` + $errors"
), array(
'Traction.profile_id'=>$profile['Profile']['id'],
'Traction.date'=>date('Y-m-d')
));
-- Table structure for table tractions
CREATE TABLE IF NOT EXISTS `tractions` (
`id` int(10) NOT NULL auto_increment,
`date` date default NULL,
`profile_id` int(10) default NULL,
`followings` int(10) default '0',
`unfollowings` int(10) default '0',
`favorites` int(10) default '0',
`retweets` int(10) default '0',
`thanks` int(10) default '0',
`errors` int(10) default '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
you need to specify the tableName, since multiple tables contains the same column name,
"Traction.favorites"=>"Traction.`favorites` + $favorites"
Apparently both of the tables contain the column favorites, might also contain the column errors
As you are joining a second table you probably want to set Traction.favorites = Profile.favorites + $favorites and Traction.errors = Profile.errors + $errors
As said by JW. you need to use a full identifier
if u Use in WhereHas
Example
enter code here $courses=Course::where('status',1)
->with('categories')
->whereHas('categories',function ($query)use ($request){
return $query->where('categories.id',$request->categories_id);
})->get();
if u have filter Course by Categoriy_id
use 'relationname'=> for example categories
u must call categories.id instead of id
Related
Schema:
CREATE TABLE IF NOT EXISTS `user` (
`id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`deleted` TIMESTAMP NOT NULL,
`email` VARCHAR(254) NOT NULL UNIQUE
);
CREATE TABLE IF NOT EXISTS `userVersion` (
`userId` BIGINT UNSIGNED,
`effective` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`created` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`name` VARCHAR(100) NOT NULL,
PRIMARY KEY (`userId`, `effective`, `created`),
FOREIGN KEY (`userId`) REFERENCES `user`(`id`)
);
The query I'm trying to perform:
SELECT u.id
FROM `user` u
INNER JOIN userVersion uv
ON u.id = uv.userId
AND uv.effective = (
SELECT MAX(uv1.effective)
FROM userVersion uv1
WHERE uv1.userId = u.id
AND uv1.effective <= NOW())
AND uv.created = (
SELECT MAX(uv2.created)
FROM userVersion uv2
WHERE uv2.userId = u.id
AND uv2.effective = uv1.effective
AND uv2.created <= NOW())
I'm getting an unknown column error of uv1.effective (situated right before the last line). I believe this query works for other databases (e.g. Oracle) but doesn't seem to work with MySQL. How could I change this query to get the same behavior?
PS: The created column is supposed to represent when the row was inserted in the database while effective is supposed to represent when that row should start being used (this allows me to add changes in the present that will work in the future).
I have a MySQL query to optimized because when this query runs it increase MySQL process CPU usage to 100%. So I did some modification on that query and now it decrease its running time but still CPU usage is near to 100%. I index all search fields in both tables.
These 'milestone_and_events' and 'milestone_and_events_reminders' has one to many relationship ('milestone_and_events' have one or more 'milestone_and_events_reminders') and 'milestone_and_events_reminders' table have nearly 1.5 million records.
Is there any way to further optimize this query, can any one give me a guide ?? thank you
This is original query
SELECT MAE.*
, MAER.reminder_user_id
, MAER.reminder_type
, MAER.id as reminder_id
FROM milestone_and_events AS MAE
LEFT
JOIN milestone_and_events_reminders MAER
ON MAE.id = MAER.milestone_and_events_id
WHERE MAER.alert_time < '$currentTime'
AND MAER.issued ! = 1
AND MAE.completed = 0
GROUP
BY MAE.id
, MAER.reminder_user_id
, MAER.reminder_type
This is my current query
$currentTime = date('Y-m-d H:i:s');
$query = 'SELECT MAE.id, MAE.time, MAER.reminder_user_id, MAER.reminder_type, MAER.id AS reminder_id
FROM milestone_and_events AS MAE
LEFT JOIN milestone_and_events_reminders MAER ON
MAE.id = MAER.milestone_and_events_id AND
MAER.issued =0 AND
MAER.alert_time < "' . $currentTime . '" AND
MAE.completed =0
WHERE MAER.reminder_type != "onTime"
GROUP BY MAER.milestone_and_events_id,MAER.reminder_user_id,MAER.reminder_type';
UPDATE 1
this 'milestone_and_events' table have nearly 200 entries.
CREATE TABLE `milestone_and_events` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`description` varchar(250) NOT NULL,
`time` datetime NOT NULL,
`attached_type` int(11) DEFAULT NULL,
`attached_type_value` int(11) DEFAULT NULL,
`completed` int(11) NOT NULL DEFAULT '0',
`type` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
KEY `time` (`time`),
KEY `completed` (`completed`),
KEY `id` (`id`,`completed`)
) ENGINE=InnoDB AUTO_INCREMENT=154 DEFAULT CHARSET=utf8
'milestone_and_events_reminders' table have nearly 1.5 million entries.
CREATE TABLE `milestone_and_events_reminders` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`reminder_user_id` int(11) NOT NULL,
`milestone_and_events_id` int(11) NOT NULL,
`alert_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`issued` tinyint(11) NOT NULL DEFAULT '0',
`reminder_type` enum('upComming','delayed','onTime') NOT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`reminder_user_id`),
KEY `reminder_type` (`reminder_type`),
KEY `issued` (`issued`),
KEY `milestone_and_events_id` (`milestone_and_events_id`, `issued`,`reminder_type`),
CONSTRAINT `milestone_and_events_reminders_ibfk_1` FOREIGN KEY (`milestone_and_events_id`)
REFERENCES `milestone_and_events` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=3323544 DEFAULT CHARSET=utf8
This is your query:
SELECT MAE.id, MAE.time, MAER.reminder_user_id, MAER.reminder_type, MAER.id AS reminder_id
FROM milestone_and_events AS MAE LEFT JOIN
milestone_and_events_reminders MAER
ON MAE.id = MAER.milestone_and_events_id AND
MAER.issued = 0 AND
MAER.alert_time < "' . $currentTime . '"
WHERE MAE.completed = 0 AND MAER.reminder_type <> "onTime"
GROUP BY MAE.id, MAER.reminder_user_id, MAER.reminder_type;
Note: I moved the condition MAE.completed = 0 from the on clause to the where clause. It does nothing in the on clause. And, I changed the first key in the group by to match the SELECT.
The best indexes for this query are composite indexes : milestone_and_events(completed, id) and milestone_and_events_reminders(milestone_and_events_id, issued, alert_time, reminder_type).
My guess is that putting the completed = 0 where it belongs will reduce the amount of data and improve the performance of the query.
I want to subtract between two rows of different table:
I have created a view called leave_taken and table called leave_balance.
I want this result from both table:
leave_taken.COUNT(*) - leave_balance.balance
and group by leave_type_id_leave_type
Code of both table
-----------------View Leave_Taken-----------
CREATE ALGORITHM = UNDEFINED DEFINER=`1`#`localhost` SQL SECURITY DEFINER
VIEW `leave_taken`
AS
select
`leave`.`staff_leave_application_staff_id_staff` AS `staff_leave_application_staff_id_staff`,
`leave`.`leave_type_id_leave_type` AS `leave_type_id_leave_type`,
count(0) AS `COUNT(*)`
from
(
`leave`
join `staff` on((`staff`.`id_staff` = `leave`.`staff_leave_application_staff_id_staff`))
)
where (`leave`.`active` = 1)
group by `leave`.`leave_type_id_leave_type`;
----------------Table leave_balance----------
CREATE TABLE IF NOT EXISTS `leave_balance` (
`id_leave_balance` int(11) NOT NULL AUTO_INCREMENT,
`staff_id_staff` int(11) NOT NULL,
`leave_type_id_leave_type` int(11) NOT NULL,
`balance` int(3) NOT NULL,
`date_added` date NOT NULL,
PRIMARY KEY (`id_leave_balance`),
UNIQUE KEY `id_leave_balance_UNIQUE` (`id_leave_balance`),
KEY `fk_leave_balance_staff1` (`staff_id_staff`),
KEY `fk_leave_balance_leave_type1` (`leave_type_id_leave_type`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
------- Table leave ----------
CREATE TABLE IF NOT EXISTS `leave` (
`id_leave` int(11) NOT NULL AUTO_INCREMENT,
`staff_leave_application_id_staff_leave_application` int(11) NOT NULL,
`staff_leave_application_staff_id_staff` int(11) NOT NULL,
`leave_type_id_leave_type` int(11) NOT NULL,
`date` date NOT NULL,
`active` int(11) NOT NULL DEFAULT '1',
`date_updated` date NOT NULL,
PRIMARY KEY (`id_leave`,`staff_leave_application_id_staff_leave_application`,`staff_leave_application_staff_id_staff`),
KEY `fk_table1_leave_type1` (`leave_type_id_leave_type`),
KEY `fk_table1_staff_leave_application1` (`staff_leave_application_id_staff_leave_application`,`staff_leave_application_staff_id_staff`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=32 ;
Well, I still don't think you've provided enough information. It would be very helpful to have some sample data and your expected output (in tabular format). That said, I may have something you can start working with. This query finds all staff members, calculates their current leave (grouped by type), and determines the difference between that and their balance by leave type. Take a look at it, and more importantly (perhaps) the sqlfiddle here that I used which has the sample data in it (very important to determining if this is the correct path for your data).
SELECT
staff.id_staff,
staff.name,
COUNT(`leave`.id_leave) AS leave_count,
leave_balance.balance,
(COUNT(`leave`.id_leave) - leave_balance.balance) AS leave_difference,
`leave`.leave_type_id_leave_type AS leave_type
FROM
staff
JOIN `leave` ON staff.id_staff = `leave`.staff_leave_application_staff_id_staff
JOIN leave_balance ON
(
staff.id_staff = leave_balance.staff_id_staff
AND `leave`.leave_type_id_leave_type = leave_balance.leave_type_id_leave_type
)
WHERE
`leave`.active = 1
GROUP BY
staff.id_staff, leave_type;
Good luck!
I'm trying to SELECT, and get a unique result set, from a MySQL database, as shown below. My problem is, I think, I don't understand LEFT Joins well enough. Or, maybe I need to use a different Join approach.
Here's a description of the database.
tbAdult (Adults) have x number of tbchild (Children) , and uses a cross-ref table called tbadultchildxref. This table has an f-key to both Adult and Child. I have to use an x-ref table, because there's a many-to-many relationship between these two tables, and there's other data that's keep in the x-ref, which I have removed for simplicity.
In turn, each Child belongs to a Program (tblprogram).
Each Program has x number of Cameras (tblCamera). Again, I have to use an x-ref table between tblProgram and tblCamera due to a many-to-many relationship, and other reasons.
What I am trying to get at, is a unique list of Cameras for a given Parent.
For example, Parent 675 has three children, Child ID's 789,788, and 789. Those three children, in turn, belong to Program ID's 4, 5, and 6.
Program ID 4 has Camera ID's 1,2,3
Program ID 5 has Camera ID's 4,5,6
Program ID 6 has Camera ID's 1,6,7,8
What I would like the result set to be is 1,2,3,4,5,6,7,8
I have tried different combinations of SELECT DISTINCT, LEFT JOINS on the various x-ref tables, etc. but I just can't seem to get it.
My other problem, along the way, is I need to check the "Active" fields in Adult, Child, and Program to equal = 1 (true) for the result set.
Thanks in advance.
CREATE TABLE `tbladult` (
`pkAdultID` int(11) NOT NULL AUTO_INCREMENT,
`fldAdultActive` tinyint(1) DEFAULT '1',
`fldAdultLogin` varchar(30) DEFAULT NULL,
`fldAdultPassword` varchar(45) DEFAULT NULL,
`fldAdultFirstName` varchar(60) DEFAULT NULL,
`fldAdultLastName` varchar(60) DEFAULT NULL,
PRIMARY KEY (`pkAdultID`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
/*Table structure for table `tblchild` */
CREATE TABLE `tblchild` (
`pkChildID` int(11) NOT NULL AUTO_INCREMENT,
`fldChildActive` tinyint(4) DEFAULT NULL,
`fldChildFirstName` varchar(45) DEFAULT NULL,
`fldChildLastName` varchar(45) DEFAULT NULL,
`fkChildProgram` int(1) DEFAULT NULL,
PRIMARY KEY (`pkChildID`),
KEY `FK_tblchild` (`fkChildProgram`),
CONSTRAINT `FK_tblchild` FOREIGN KEY (`fkChildProgram`) REFERENCES `tblprogram` (`pkProgramID`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
/*Table structure for table `tbladultchildxref` */
CREATE TABLE `tbladultchildxref` (
`pkAdultChildxRefID` int(11) NOT NULL AUTO_INCREMENT,
`fldAdultChildxRefActive` tinyint(1) DEFAULT '1',
`fkAdultID` int(11) DEFAULT NULL,
`fkChildID` int(11) DEFAULT NULL,
PRIMARY KEY (`pkAdultChildxRefID`),
KEY `FK_tbladultchildxref` (`fkAdultID`),
KEY `FK_tbladultchildxref2` (`fkChildID`),
CONSTRAINT `FK_tbladultchildxref` FOREIGN KEY (`fkAdultID`) REFERENCES `tbladult` (`pkAdultID`),
CONSTRAINT `FK_tbladultchildxref2` FOREIGN KEY (`fkChildID`) REFERENCES `tblchild` (`pkChildID`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
/*Table structure for table `tblprogram` */
CREATE TABLE `tblprogram` (
`pkProgramID` int(11) NOT NULL AUTO_INCREMENT,
`fldProgamActive` tinyint(1) DEFAULT '1',
`fldProgramName` varchar(50) DEFAULT NULL,
PRIMARY KEY (`pkProgramID`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;
/*Table structure for table `tblcamera` */
CREATE TABLE `tblcamera` (
`pkCameraID` int(11) NOT NULL AUTO_INCREMENT,
`fldCameraName` varchar(50) DEFAULT NULL,
`fldCameralocation` varchar(50) DEFAULT NULL,
`fldCameraURL` varchar(250) DEFAULT NULL,
PRIMARY KEY (`pkCameraID`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
/*Table structure for table `tblprogramcameraxref` */
CREATE TABLE `tblprogramcameraxref` (
`pkProgramCameraXrefID` int(11) NOT NULL AUTO_INCREMENT,
`fkProgramID` int(11) DEFAULT NULL,
`fkCameraID` int(11) DEFAULT NULL,
PRIMARY KEY (`pkProgramCameraXrefID`),
KEY `FK_tblprogramcameraxref` (`fkProgramID`),
KEY `FK_camerasforprograms` (`fkCameraID`),
CONSTRAINT `FK_camerasforprograms` FOREIGN KEY (`fkCameraID`) REFERENCES `tblcamera` (`pkCameraID`),
CONSTRAINT `FK_tblprogramcameraxref` FOREIGN KEY (`fkProgramID`) REFERENCES `tblprogram` (`pkProgramID`)
No LEFT JOINs necessary:
SELECT DISTINCT tblprogramcameraxref.fkcameraid
FROM tblprogramcameraxref
JOIN tblprogram ON tblprogramcameraxref.fkprogramid = tblprogram.pkprogramid
AND tblprobram.fldProgramActive = 1
JOIN tblchild ON tblprogramcameraxref.fkprogramid = tblchild.fkchildprogram
AND tblchild.fldChildActive = 1
JOIN tbladultchildxref ON tblchild.pkchildid = tbladultchildxref.fkchildid
AND tbladultchildxref.fldAdultChildxRefActive = 1
WHERE tbladultchildxref.fkadultid = 675
Also, you may want to check the fkChildProgram int(1) DEFAULT NULL, in tblchild - the column it references is defined as int(11)
At this point you shouldn't really need to check if Adult is active (since that's the search criteria you started with), but if you must - just add this to the end of the join list:
JOIN tbladult ON tbladultchildxref.fkadultid = tbladult.pkadultid
AND tbladult.fldAdultActive = 1
It is a long description. If I have understood the question correctly this query should help you -
SELECT DISTINCT pcref.fkCameraID
FROM tbladult adult,
tblchild child,
tbladultchildxref acref,
tblprogram prog,
tblcamera camera,
tblprogramcameraxref pcref
WHERE adult.pkAdultID = 675
AND adult.fldAdultActive = TRUE
AND adult.pkAdultID = acref.fkAdultID
AND acref.fkChildID = child.pkChildID
AND child.fldChildActive = TRUE
AND child.fkChildProgram = prog.pkProgramID
AND prog.fldProgamActive = TRUE
AND prog.pkProgramID = pcref.fkProgramID
Having some troubles with ON DUPLICATE KEY UPDATE in MySQL. Below is the query im trying to run.
INSERT INTO `Overall` ( `rsn` , `starting_xp` , `starting_lvl` ) VALUES ( 'iWader' , '195843626' , '2281' ) ON DUPLICATE KEY UPDATE `current_xp` = '195843626' AND `current_lvl` = '2281'
It inserts fine, but when there is a duplicate it doesnt update, and doesnt throw any errors.
Running the query through PMA returns no error and doesnt update
Removing the ON DUPLICATE KEY UPDATE section returns a duplicate key error
This is the structure of my table
CREATE TABLE IF NOT EXISTS `overall` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`rsn` varchar(12) NOT NULL,
`starting_xp` int(10) unsigned NOT NULL,
`starting_lvl` int(10) unsigned NOT NULL,
`current_xp` int(10) unsigned NOT NULL,
`current_lvl` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `rsn` (`rsn`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
After ON DUPLICATE KEY UPDATE you should not need to use and with the fields, use , instead.
ON DUPLICATE KEY UPDATE `current_xp` = '195843626', `current_lvl` = '2281'
Try this:
INSERT INTO `Overall` ( `rsn` , `starting_xp` , `starting_lvl` ) VALUES ( 'iWader' , '195843626' , '2281' ) ON DUPLICATE KEY UPDATE `current_xp` = '195843626', `current_lvl` = '2281';
The AND in your UPDATE clause is wrong. The AND is used in boolean expressions like "is foo true AND bar true?"
Here you want to update column current_xp, current_lvl.