Executing multiple join with expressions on Zend Framework 2 - php

Actually I'm working on a project and I'm looking on how Zend Framework 2 handle complex queries (expecially on how to join n:m tables and how to use GROUP_CONCAT and other functions). Do you know the best practice to execute this query:
SELECT o. * , x.group_one, x.group_two
FROM table_one AS o
LEFT JOIN (
SELECT r.fk1, GROUP_CONCAT( t.field_one ) AS group_one, GROUP_CONCAT( t.field_two ) AS group_two
FROM table_three AS r
INNER JOIN table_two AS t ON r.fk2 = t.id
GROUP BY r.fk1
) AS x ON o.id = x.fk1
LIMIT 0 , 20;
using this db schema:
--
-- Database: `table-test-1`
--
-- --------------------------------------------------------
--
-- Structure of table `table_one`
--
CREATE TABLE `table_one` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`field_1` varchar(255) NOT NULL,
`field_2` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
--
-- Dump for table `table_one`
--
INSERT INTO `table_one` (`id`, `field_1`, `field_2`) VALUES
(1, 'baz', 'bat'),
(2, 'foo', 'bar'),
(3, 'foo2', 'bat2'),
(4, 'fuz', 'bar2'),
(5, 'poo', 'pee');
-- --------------------------------------------------------
--
-- Structure of table `table_three`
--
CREATE TABLE `table_three` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`fk1` bigint(20) NOT NULL,
`fk2` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk1` (`fk1`,`fk2`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;
--
-- Dump for table `table_three`
--
INSERT INTO `table_three` (`id`, `fk1`, `fk2`) VALUES
(5, 1, 1),
(1, 1, 2),
(6, 1, 4),
(2, 2, 2),
(4, 3, 2),
(7, 3, 3),
(3, 4, 1),
(8, 5, 3),
(9, 5, 4);
-- --------------------------------------------------------
--
-- Structure of table `table_two`
--
CREATE TABLE `table_two` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`field_one` varchar(255) NOT NULL,
`field_two` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
--
-- Dump for table `table_two`
--
INSERT INTO `table_two` (`id`, `field_one`, `field_two`) VALUES
(1, 'label_name_1', 'label_extended_name_1'),
(2, 'label_name_2', 'label_extended_name2'),
(3, 'label_name_3', 'label_extended_name_3'),
(4, 'label_name_4', 'label_extended_name4');
At the moment I solved using a Zend\Db\Sql\Sql statement with an handmade query, but I'd like to know if there is, actually, a way to do this with a native Select() (possibly without using Doctrine or similar).
Thank you in advance :)

You have to import use Zend\Db\Sql\Predicate\Expression; to use group_concat.
Ex:
$sql = new Sql($this->adapter);
$select = $sql->select();
$select->columns(array('*'));
$select->from('tblCGii');
$select->join("tblCGFieldValues", "tblCGii.id = tblCGFieldValues.Cgii_id", array("field_values"=>new Expression("Group_Concat(tblCGFieldValues.field_values)")),"LEFT");
$select->group('tblCGii.id');

Related

Searching String In All Tables And Displaying It With Pagination Technique

My database has five tables:
Computer|Mobile|Tablet|MusicSystem|Camera
All the tables are in same structure like:
productID|ProductBrand|Price|userId
Here, I want to search product's brand name in all the field of productBrand in all the tables, where userId=$userId and limit is 10 then display it with pagination technique.
How can I create such a query in MySQLi and display it in PHP?
Thanks in advance.
try this...
select * from Computer c,Mobile m,Tablet t,MusicSystem ms,Camera cam where userId = $userId and (c.productBrand = searchingProductBrand or m.productBrand = searchingProductBrand or t.productBrand = searchingProductBrand or ms.productBrand = searchingProductBrand or cam.productBrand = searchingProductBrand ) limit 10
If you are following same structure for each category table,better structure is to categorize all into single table as follows
CREATE TABLE IF NOT EXISTS `category` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`category_name` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
INSERT INTO `category` (`id`, `category_name`) VALUES
(1, 'Computer'),
(2, 'Mobile'),
(3, 'Tablet'),
(4, 'MusicSystem'),
(5, 'Camera');
CREATE TABLE IF NOT EXISTS `products` (
`ProductId` int(11) NOT NULL AUTO_INCREMENT,
`ProductBrand` varchar(255) NOT NULL,
`Price` varchar(100) NOT NULL,
`UserId` int(11) unsigned NOT NULL,
`CatId` int(11) unsigned NOT NULL,
PRIMARY KEY (`ProductId`),
KEY `CatId` (`CatId`),
CONSTRAINT `products_ibfk_1` FOREIGN KEY (`CatId`) REFERENCES `category` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
INSERT INTO `products` (`ProductId`, `ProductBrand`, `Price`, `UserId`, `CatId`) VALUES
(1, 'Lenovo', '35000', 5, 3),
(2, 'Asus', '28350', 5, 2),
(3, 'Dell', '25000', 5, 3),
(4, 'MotoG', '12500', 5, 2),
(5, 'Samsung', '52000', 4, 1);
You can get your result using
SELECT * FROM products WHERE userId = '5'
Check the FIDDLE

hand-coded table name works but dynamic name is not working in PHP

This is the code That is supposed to delete the student from the table.
but it it not working.
it only works when the table name is static.
public function delete_student($student_class,$school_session,$id){
$table_name = $student_class."_".$school_session."_student_record";
$pdo = KITE::getInstance('pdo');
$query = $pdo->prepare("DELETE FROM ".$table_name." WHERE `id` = ?");
$query->bindValue(1,$id);
$query->execute();
}
As I mentioned in a comment, a slash / is not permitted in the name of a table, column, function name, procedure name etc. Why then your query works when the name is statically typed is a mystery.
A quickly constructed demo that uses innodb tables with foreign keys and hopefully very little / no redundancy. If you have mysql running locally try running this in your gui ( such as heidi ) to get an idea of how it might be accomplished.
drop database if exists `demo_school_sessions`;
create database if not exists `demo_school_sessions`;
use `demo_school_sessions`;
drop table if exists `classes`;
create table if not exists `classes` (
`id` int(10) unsigned not null auto_increment,
`class` varchar(50) not null,
primary key (`id`)
) engine=innodb default charset=latin1;
insert into `classes` (`id`, `class`) values
(1, 'mathematics'),
(2, 'physics'),
(3, 'computing');
drop table if exists `sessions`;
create table if not exists `sessions` (
`id` int(10) unsigned not null auto_increment,
`session` varchar(50) not null,
primary key (`id`)
) engine=innodb default charset=latin1;
insert into `sessions` (`id`, `session`) values
(1, 'summer'),
(2, 'winter'),
(3, 'spring'),
(4, 'autumn');
drop table if exists `students`;
create table if not exists `students` (
`id` int(10) unsigned not null auto_increment,
`student` varchar(50) not null,
primary key (`id`)
) engine=innodb default charset=latin1;
insert into `students` (`id`, `student`) values
(1, 'bobby droptables'),
(2, 'sue select'),
(3, 'david delete'),
(4, 'valerie view'),
(5, 'peter procedure'),
(6, 'freddy function');
drop table if exists `year`;
create table if not exists `year` (
`id` int(10) unsigned not null auto_increment,
`year` varchar(9) not null default '2014/2015',
primary key (`id`)
) engine=innodb default charset=latin1;
insert into `year` (`id`, `year`) values
(1, '2014/2015'),
(2, '2015/2016'),
(3, '2016/2017'),
(4, '2017/2018'),
(5, '2018/2019'),
(6, '2019/2020');
drop table if exists `master`;
create table if not exists `master` (
`id` int(10) unsigned not null auto_increment,
`student` int(10) unsigned not null default '0',
`class` int(10) unsigned not null default '0',
`session` int(10) unsigned not null default '0',
`year` int(10) unsigned not null default '0',
primary key (`id`),
key `student` (`student`),
key `class` (`class`),
key `session` (`session`),
key `year` (`year`),
constraint `fk_year` foreign key (`year`) references `year` (`id`) on delete cascade on update cascade,
constraint `fk_class` foreign key (`class`) references `classes` (`id`) on delete cascade on update cascade,
constraint `fk_session` foreign key (`session`) references `sessions` (`id`) on delete cascade on update cascade,
constraint `fk_stud` foreign key (`student`) references `students` (`id`) on delete cascade on update cascade
) engine=innodb default charset=latin1;
insert into `master` (`id`, `student`, `class`, `session`, `year`) values
(1, 1, 3, 4, 1),
(2, 1, 1, 4, 1),
(3, 2, 2, 4, 1),
(4, 2, 3, 4, 1);
/* example to select records */
select * from `master` m
left outer join `students` s on s.id=m.student
left outer join `classes` c on c.id=m.class
left outer join `sessions` ss on ss.id=m.`session`
left outer join `year` y on y.id=m.`year`
where m.`class`=3 or m.`student`=1;
/* Example to delete a student */
delete from `students` where `id`=1;
/*
*/
Because of Foreign Key Dependencies if you delete a student from the student table all associated records from the master table would also be deleted as there is a cascade set for both update and delete. Care MUST be taken when using foreign keys like this because if you delete a record accidentally and there is a cascaded delete set on the key then all the associated records disappear!

get the count of field and group concat

table structure is as follows
-- Table structure for table category
CREATE TABLE `category` (
`cat_id` int(10) NOT NULL auto_increment,
`heading` varchar(255) NOT NULL,
PRIMARY KEY (`cat_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
INSERT INTO `category` (`cat_id`, `heading`) VALUES
(1, 'Fashion'),
(2, 'Kids');
-- --------------------------------------------------------
-- Table structure for table `shop`
CREATE TABLE `shop` (
`store_id` int(10) NOT NULL auto_increment,
`shop_name` varchar(255) NOT NULL,
`cat_id` int(10) NOT NULL,
`subcat_id` int(10) NOT NULL,
PRIMARY KEY (`store_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
INSERT INTO `shop` (`store_id`, `shop_name`, `cat_id`, `subcat_id`) VALUES
(1, 'Test Store', 1, 1),
(2, 'Test Store 1', 1, 1),
(3, 'Another Store', 1, 3);
-- --------------------------------------------------------
-- Table structure for table `subcategory`
CREATE TABLE `subcategory` (
`subcat_id` int(10) NOT NULL auto_increment,
`cat_id` int(10) NOT NULL,
`heading` varchar(255) NOT NULL,
PRIMARY KEY (`subcat_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
INSERT INTO `subcategory` (`subcat_id`, `cat_id`, `heading`) VALUES
(1, 1, 'Women'),
(2, 1, 'General'),
(3, 1, 'Men'),
(4, 2, 'Children');
if i use the below query i get the following output
SELECT
`category`.`heading` AS `category`
, `subcategory`.`heading` AS `subcategory`
, COUNT(`shop`.`subcat_id`) AS cnt
FROM
`test`.`shop`
INNER JOIN `test`.`subcategory`
ON (`shop`.`subcat_id` = `subcategory`.`subcat_id`)
INNER JOIN `test`.`category`
ON (`shop`.`cat_id` = `category`.`cat_id`)
GROUP BY `shop`.`subcat_id`
HAVING (COUNT(`shop`.`subcat_id`) !='');
categorysubcategorycnt
FashionWomen2
FashionMen1
but i want to group concat the subcategory like below
categorysubcategory
FashionWomen,2|Men,1
Try this
SELECT t.category,
GROUP_CONCAT(CONCAT(t.subcategory,',',t.cnt) SEPARATOR '|') `concat`
FROM (
SELECT
`category`.`heading` AS `category`
, `subcategory`.`heading` AS `subcategory`
, COUNT(`shop`.`subcat_id`) AS cnt
FROM
`shop`
INNER JOIN `subcategory`
ON (`shop`.`subcat_id` = `subcategory`.`subcat_id`)
INNER JOIN `category`
ON (`shop`.`cat_id` = `category`.`cat_id`)
GROUP BY `shop`.`subcat_id`
) t
GROUP BY t.category
Note group concat has a default limit of 1024 character but it can be increased by following the manual
Fiddle Demo
Not a recommended output format, but easily done with a nested subquery:
SELECT category,
group_concat(subcategory, ',', cnt separator '|') as vals
FROM (SELECT c.`heading` AS `category`, sc.`heading` AS `subcategory`,
COUNT(`shop`.`subcat_id`) AS cnt
FROM `test`.`shop` s INNER JOIN
`test`.`subcategory` sc
ON s.`subcat_id` = sc.`subcat_id`) INNER JOIN
`test`.`category` c
ON s.`cat_id` = c.`cat_id`
GROUP BY c.`heading`, sc.`heading`
) sc
GROUP BY category;
Your having clause is unnecessary. It is just checking that there is at least one row for each group. But there is one, because you are using inner join.

How would I modify this MySQL Query to find a rank?

I am developing a league application in PHP. When I visit the ladder view page, I have a query that selects all the squads from that ladder and orders them by their experience(league_experience). I want to modify the query so that it finds the rank of the current squad.
$query_squads = "
SELECT
s.squad_id AS squad_id, s.ladder_id, s.team_id AS team_id,
x.experience_id, x.squad_id, SUM(x.value) as total_exp
FROM league_squads AS s
LEFT JOIN league_experience AS x ON (s.squad_id = x.squad_id)
WHERE s.ladder_id = ".$ladder_id."
GROUP BY s.squad_id, s.ladder_id, s.team_id, x.experience_id, x.squad_id
ORDER BY total_exp DESC
";
Here's my tables
--
-- Table structure for table `league_experience`
--
CREATE TABLE IF NOT EXISTS `league_experience` (
`experience_id` int(15) NOT NULL,
`squad_id` int(15) NOT NULL,
`value` int(15) NOT NULL,
`date_earned` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`description` varchar(255) NOT NULL,
PRIMARY KEY (`experience_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `league_experience`
--
INSERT INTO `league_experience` (`experience_id`, `squad_id`, `value`, `date_earned`, `description`) VALUES
(1, 1, 500, '2013-09-03 07:10:59', 'For being ballers.'),
(2, 2, 250, '2013-09-03 07:10:52', 'For being awesome.');
-- --------------------------------------------------------
--
-- Table structure for table `league_squads`
--
CREATE TABLE IF NOT EXISTS `league_squads` (
`squad_id` int(15) NOT NULL AUTO_INCREMENT,
`team_id` int(15) NOT NULL,
`ladder_id` int(15) NOT NULL,
`date_joined` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`status` tinyint(1) NOT NULL,
`last_rank` tinyint(5) NOT NULL,
PRIMARY KEY (`squad_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `league_squads`
--
INSERT INTO `league_squads` (`squad_id`, `team_id`, `ladder_id`, `date_joined`, `status`, `last_rank`) VALUES
(1, 1, 1, '2013-09-03 08:16:27', 0, 1),
(2, 2, 1, '2013-09-03 08:16:25', 0, 2);
SELECT
s.squad_id AS squad_id, s.ladder_id, s.team_id AS team_id,
x.experience_id, x.squad_id, SUM(x.value) as total_exp,
#i:=#i+1 AS rank
FROM league_squads AS s
LEFT JOIN league_experience AS x ON (s.squad_id = x.squad_id),
(SELECT #i:=0) AS foo
WHERE s.ladder_id = 1
GROUP BY s.squad_id, s.ladder_id, s.team_id, x.experience_id, x.squad_id
ORDER BY total_exp DESC
sample fiddle

Duplicate row in database with relational tables

I am trying to duplicate a page in the database and all related rows.
The problem I am having is because the page_group_id is an identifier for both tables. Is there any way of doing this without looping each of the new "page_groups" records?
pages (page_id, page_name, etc)
page_groups (page_group_id, page_id, etc)
page_group_items (page_group_id, item_id, etc)
UPDATE:
--
-- Table structure for table `pages`
--
CREATE TABLE IF NOT EXISTS `pages` (
`page_id` int(11) NOT NULL AUTO_INCREMENT,
`page_name` varchar(255) NOT NULL,
PRIMARY KEY (`page_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `pages`
--
INSERT INTO `pages` (`page_id`, `page_name`) VALUES
(1, 'My Page'),
(2, 'My other page');
-- --------------------------------------------------------
--
-- Table structure for table `page_groups`
--
CREATE TABLE IF NOT EXISTS `page_groups` (
`page_group_id` int(11) NOT NULL AUTO_INCREMENT,
`page_group_name` varchar(255) NOT NULL,
`page_id` int(11) NOT NULL,
PRIMARY KEY (`page_group_id`),
KEY `page_id` (`page_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `page_groups`
--
INSERT INTO `page_groups` (`page_group_id`, `page_group_name`, `page_id`) VALUES
(1, 'My Group', 1),
(2, 'My Group', 2);
-- --------------------------------------------------------
--
-- Table structure for table `page_group_items`
--
CREATE TABLE IF NOT EXISTS `page_group_items` (
`page_group_id` int(11) NOT NULL,
`item_id` int(11) NOT NULL,
KEY `item_id` (`item_id`),
KEY `page_group_id` (`page_group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `page_group_items`
--
INSERT INTO `page_group_items` (`page_group_id`, `item_id`) VALUES
(1, 1),
(1, 2),
(1, 3),
(2, 1),
(2, 2);
Since you're replacing the only unique identifier in each table (the primary key) when copying, I can't see a way of doing it without adding temporary anchor columns to the tables, do the copy and remove them again. Something like this;
ALTER TABLE pages ADD originalpageid INT;
UPDATE pages set originalpageid=page_id;
ALTER TABLE page_groups ADD originalpagegroupid INT;
UPDATE page_groups SET originalpagegroupid=page_group_id;
INSERT INTO pages (page_name,originalpageid)
SELECT page_name,originalpageid FROM pages;
INSERT INTO page_groups (page_group_name,page_id,originalpagegroupid)
SELECT page_group_name,MAX(pages.page_id),originalpagegroupid
FROM page_groups
JOIN pages
ON page_groups.page_id=originalpageid
GROUP BY originalpageid,page_group_name,originalpagegroupid;
INSERT INTO page_group_items(page_group_id,item_id)
SELECT MAX(page_groups.page_group_id),item_id
FROM page_group_items
JOIN page_groups
ON page_group_items.page_group_id=originalpagegroupid
GROUP BY originalpagegroupid,item_id;
ALTER TABLE pages DROP COLUMN originalpageid;
ALTER TABLE page_groups DROP COLUMN originalpagegroupid;
An SQLfiddle to test with
If the use case is doing it all the time in the system, it may not be the solution you're looking for, but for manual intervention it should work well.
As always, always back your database up before running SQL from random strangers on the Internet :)

Categories