Query consult doctrine composite key - php

Hello I am try to take data with dictrine 2.5 from a table but I do not know how to do it.
SQL FILE
DROP DATABASE composite_key;
CREATE DATABASE composite_key;
USE composite_key;
CREATE TABLE company (
id_company INT NOT NULL AUTO_INCREMENT,
name VARCHAR(200) NULL,
PRIMARY KEY (id_company)
);
CREATE TABLE users (
id_user INT NOT NULL AUTO_INCREMENT,
name VARCHAR(200) null,
PRIMARY KEY (id_user)
);
CREATE TABLE reference (
id_company INT NOT NULL,
id_user INT NOT NULL,
createat TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id_company),
FOREIGN KEY (id_company) REFERENCES company(id_company),
FOREIGN KEY (id_user) REFERENCES users(id_user)
);
INSERT INTO users (`name`) VALUES ('test1');
INSERT INTO users (`name`) VALUES ('test2');
INSERT INTO users (`name`) VALUES ('test3');
INSERT INTO company (`name`) VALUES ('company1');
INSERT INTO company (`name`) VALUES ('company2');
INSERT INTO company (`name`) VALUES ('company3');
INSERT INTO reference (`id_company`,`id_user`) VALUES (1,1);
INSERT INTO reference (`id_company`,`id_user`) VALUES (2,2);
INSERT INTO reference (`id_company`,`id_user`) VALUES (3,3);
QUERY WHICH I HAVE TRIED
$dql = "SELECT r FROM CompositeBundle:Reference r JOIN CompositeBundle:Company c JOIN CompositeBundle:Users u WHERE c.idCompany = 1 AND u.idUser = 1 ";
$dql = "SELECT r FROM CompositeBundle:Reference r JOIN r.idUser JOIN r.idCompany WHERE r.idCompany = 1 AND r.idUser = 1 ";
If somone know to do these dql please sat It.

Related

JOIN Call table to grouping by Origination and Destination

It's a call system.
tables structure and data sample :
tbl_countries
CREATE TABLE tbl_countries
(
country_id int(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
country varchar(100) NOT NULL
);
INSERT INTO tbl_countries (country) VALUES ('Austria');
INSERT INTO tbl_countries (country) VALUES ('Brazil');
INSERT INTO tbl_countries (country) VALUES ('Denmark');
INSERT INTO tbl_countries (country) VALUES ('France');
INSERT INTO tbl_countries (country) VALUES ('Lebanon');
tbl_destination_clients
CREATE TABLE tbl_destination_clients
(
des_client_id int(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
des_client varchar(100) NOT NULL,
country_id int(11) NOT NULL,
CONSTRAINT tbl_destination_clients_ibfk_1 FOREIGN KEY (country_id) REFERENCES tbl_countries (country_id)
);
CREATE INDEX country_id ON tbl_destination_clients (country_id);
INSERT INTO tbl_destination_clients (des_client, country_id) VALUES ('961123456', 5);
INSERT INTO tbl_destination_clients (des_client, country_id) VALUES ('55123456', 2);
INSERT INTO tbl_destination_clients (des_client, country_id) VALUES ('45123456', 3);
tbl_origination_clients
CREATE TABLE tbl_origination_clients
(
orig_client_id int(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
orig_client varchar(100) NOT NULL,
country_id int(11) NOT NULL,
CONSTRAINT tbl_origination_clients_ibfk_1 FOREIGN KEY (country_id) REFERENCES tbl_countries (country_id)
);
CREATE INDEX country_id ON tbl_origination_clients (country_id);
INSERT INTO tbl_origination_clients (orig_client, country_id) VALUES ('33123456', 4);
INSERT INTO tbl_origination_clients (orig_client, country_id) VALUES ('43123456', 1);
tbl_calls
CREATE TABLE tbl_calls
(
call_id int(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
orig_client_id int(11) NOT NULL,
des_client_id int(11) NOT NULL,
call_duration int(11) NOT NULL,
CONSTRAINT tbl_calls_ibfk_1 FOREIGN KEY (orig_client_id) REFERENCES tbl_origination_clients (orig_client_id),
CONSTRAINT tbl_calls_ibfk_2 FOREIGN KEY (des_client_id) REFERENCES tbl_destination_clients (des_client_id)
);
CREATE INDEX des_client_id ON tbl_calls (des_client_id);
CREATE INDEX orig_client_id ON tbl_calls (orig_client_id);
INSERT INTO tbl_calls (orig_client_id, des_client_id, call_duration) VALUES (1, 3, 5);
INSERT INTO tbl_calls (orig_client_id, des_client_id, call_duration) VALUES (1, 3, 6);
INSERT INTO tbl_calls (orig_client_id, des_client_id, call_duration) VALUES (2, 4, 9);
INSERT INTO tbl_calls (orig_client_id, des_client_id, call_duration) VALUES (1, 1, 10);
Attemps :
I try to use JOIN between the same table tbl_calls, but it's not working ;(
But the Result of this sample data must be :
Origination Destination SUM(call_duration)
------------+------------+----------------
France Brazil 11 -> (5+6)
Austria Denmark 9
France Lebanon 10
Thank you #forpas
Query with adapted join column
select
co.country Origination,
cd.country Destination,
sum(c.call_duration) TotalDuration
from tbl_calls c
inner join tbl_destination_clients d on d.des_client_id = c.des_client_id
inner join tbl_origination_clients o on o.orig_client_id = c.orig_client_id
inner join tbl_countries cd on cd.country_id = d.country_id
inner join tbl_countries co on co.country_id = o.country_id
group by
co.country,
cd.country;
With grouping after multiple joins (tbl_countries must be joined twice for destination and origination)
and sum over call_duration:
select
co.country Origination,
cd.country Destination,
sum(c.call_duration) TotalDuration
from tbl_calls c
inner join tbl_destination_clients d on d.des_client_id = c.des_client_id
inner join tbl_origination_clients o on o.orig_client_id = c.des_client_id
inner join tbl_countries cd on cd.country_id = d.country_id
inner join tbl_countries co on co.country_id = o.country_id
group by
co.country,
cd.country;

I have two values on the first row of the table it_courses field course_branch. When i join branch table with it_courses table result only one value

these are the tables
and it results
I have two values on the first row of the table it_courses field course_branch. When i join branch table with it_courses table result is like this.it doesnt shows the second value in the 1st row that was entered in the it_coursestable
Query :
SELECT branch.branch_name
FROM `branch`
JOIN `it_courses` ON it_courses.course_branch = branch.branch_id
You can try this database structure and query :
Table:
tbl_it_courses (id - PK, cource_name, branch_id - FK)
tbl_branch (id - PK, branch_id, branch_name)
Table : tbl_it_courses
CREATE TABLE tbl_it_courses (
id INT NOT NULL AUTO_INCREMENT,
cource_name VARCHAR(70) NOT NULL,
branch_id VARCHAR(70) DEFAULT NULL,
PRIMARY KEY(id)
);
INSERT INTO `tbl_it_courses` (`Id`, `cource_name`, `branch_id`) VALUES (NULL, 'PHP', '1,4'),(NULL, '.NET', '3'), (NULL, 'CCNA', '3');
Table : tbl_branch
CREATE TABLE tbl_branch (
id INT NOT NULL AUTO_INCREMENT,
branch_id VARCHAR(70) DEFAULT NULL,
branch_name VARCHAR(70) NOT NULL,
PRIMARY KEY(id)
);
INSERT INTO `tbl_branch` (`Id`, `branch_id`, `branch_name`) VALUES (NULL, 'Ern', 'brn92b224'),(NULL, 'Klm', 'brnaf3650'), (NULL, 'Tvm', 'brn272493');
Query :
SELECT tbl_it_courses.cource_name, (SELECT GROUP_CONCAT(tbl_branch.branch_name) FROM tbl_branch WHERE FIND_IN_SET(tbl_branch.id,tbl_it_courses.branch_id) > 0) as branch_name
FROM tbl_it_courses
I Hope It will Help you.
Try this.
SELECT branch.branch_id,it_courses.course_name
FROM `branch`
left JOIN `it_courses` ON LOCATE(branch.branch_name,it_courses.course_branch) > 0

How to check if values from the table 1 are used in table 2?

I have two tables employees and departments.
Departments:
create table dept (
id INT NOT NULL AUTO_INCREMENT,
dept_name VARCHAR(50) NOT NULL,
PRIMARY KEY (id)
);
Employees:
create table department_master (
id INT NOT NULL AUTO_INCREMENT,
dept_id INT NOT NULL,
emp_name VARCHAR(100) NOT NULL,
PRIMARY KEY (id)
);
I want to prevent departments being deleted from the UI if they are assigned to one of the employees in employee table. Left join is giving me duplicate values.
How do I see if the departments are being used in the employees table.
If you want to prevent a department from being deleted, you can simply add a foreign constraint to the table department_master for dept_id column.
create table department_master (
id INT NOT NULL AUTO_INCREMENT,
dept_id INT NOT NULL,
emp_name VARCHAR(100) NOT NULL,
PRIMARY KEY (id),
constraint con_dm foreign key dept_id references dept( id )
);
It's default behavior is ON DELETE RESTRICT which means that if there is atleast one row present in the department_master for a given dept_id, it can't be deleted from dept table.
If you want to fetch, the department that don't have any employee record, you can use NOT EXISTS:
select *
from dept d
where not exists (
select 1
from department_master m
where d.id = m.dept_id
);
I believe you want a count of the number of employees grouped by the department, like so:
SELECT count(*) as employees, d.dept_name FROM dept AS d LEFT JOIN department_master AS e ON e.dept_id = d.id group by e.dept_id

Symfony2 MySQL: INSERT SELECT syntax error

I am having problems with writing correct MySql query. I want to insert new collection for every user with id higher than 1000 but less than 10000.
$conn = $this->em->getConnection();
$stmt = $conn->prepare('INSERT INTO collection (name, type)
values(:name, :type)
SELECT * FROM user WHERE id<:endUser AND id>:startUser');
$stmt->bindValue('name', 'Default');
$stmt->bindValue('type', 0);
$stmt->bindValue('startUser', 1000);
$stmt->bindValue('endUser', 10000);
$stmt->execute();
This what I tried to write, but I get syntax error. Please explain me how to correct query
UPD
I should have given detailed structure of tables.
Collection
CREATE TABLE IF NOT EXISTS `collection` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`type` smallint(6) NOT NULL,
PRIMARY KEY (`id`),
KEY `IDX_FC4D6532A76ED395` (`user_id`)
);
User
CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
);
User has one-to-many relationship with Collection.
With a SELECT INTO you have to select the values you want to place in the new row and only those values. And you dont use the VALUES() clause.
As you are using static values for the new rows and not values from the user table you can do it like this.
Oh and I see in your edit you were using the wrong table name It should have been fos_user
Also as fos_user.user_id is a NOT NULL field you need to include that column in the list of fields in the insert.
$conn = $this->em->getConnection();
$stmt = $conn->prepare('INSERT INTO collection (user_id, name, type)
SELECT id, 'default', 0
FROM fos_user
WHERE id > :startUser AND id < :endUser');
$stmt->bindValue('startUser', 1000);
$stmt->bindValue('endUser', 10000);
$stmt->execute();

MySQL query doesn't work in php and works when run directly

I have a very strange problem with MySQL and PHP.
I have into a function the below query:
Global $Linker;
$query = "INSERT INTO ".$user_type." (id) VALUES (?)";
if($stmt = mysqli_prepare($Linker->DataBase,$query)) {
mysqli_stmt_bind_param($stmt,"i",$max_id);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
} printf("Error: %s.\n", mysqli_stmt_error($stmt));
where
$Linker is the variable that holds the database connection
and
$user_type is the table I want to insert in the database
I get the following error printed
Error: Cannot add or update a child row: a foreign key constraint fails (std10179db/Students, CONSTRAINT fk_Students_Users1 FOREIGN KEY (id) REFERENCES mydb.Users (Table_ID) ON DELETE NO ACTION ON UPDATE NO ACTION).
The problem is that when I run the same query directly in MySQL through command line in the server it works as expected with no errors.
I found a work around using:
$query= "SET FOREIGN_KEY_CHECKS=0";
$stmt = mysqli_prepare($Linker->DataBase,$query);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
before my query in the PHP function and another workaround would be to change my database schema
but I want to know why that is happening?
I have even tried to execute the query without parameters:
$query = "INSERT INTO Students (id) VALUES (3)";
to be exactly the same with the one I put directly into the server with no success.
Any ideas?
edit:
Table USERS
CREATE TABLE `std10179db`.`Users` (
`ID` INT NOT NULL ,
`Username` VARCHAR(45) NOT NULL ,
`Password` VARCHAR(45) NOT NULL ,
`email` VARCHAR(45) NOT NULL ,
`User_Class` VARCHAR(45) NOT NULL ,
`Table_ID` INT NOT NULL ,
PRIMARY KEY (`ID`) ,
UNIQUE INDEX `Username_UNIQUE` (`Username` ASC) ,
UNIQUE INDEX `Password_UNIQUE` (`Password` ASC) ,
UNIQUE INDEX `email_UNIQUE` (`email` ASC) )
ENGINE = InnoDB;
and Table Students
CREATE TABLE `std10179db`.`Students` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`Univ_ID` VARCHAR(45) NULL ,
`Name` VARCHAR(45) NULL ,
`Surname` VARCHAR(45) NULL ,
`Telephone` VARCHAR(45) NULL ,
`Semester` INT NULL ,
`Department_id` INT UNSIGNED NULL ,
PRIMARY KEY (`id`, `Department_id`) ,
UNIQUE INDEX `idStudents_UNIQUE` (`id` ASC) ,
INDEX `fk_Students_Department1` (`Department_id` ASC) ,
CONSTRAINT `fk_Students_Department1`
FOREIGN KEY (`Department_id` )
REFERENCES `mydb`.`Department` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Students_Users1`
FOREIGN KEY (`id` )
REFERENCES `mydb`.`Users` (`Table_ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

Categories