Multiple joins producing unwanted results - php

I'm getting weird results from my query. The numbers are way off and I can't figure out why.
Heres the table structure for the tables used in the query:
CREATE TABLE IF NOT EXISTS `bookings` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`customer_id` int(11) DEFAULT NULL,
`payment_method_id` int(11) DEFAULT NULL,
`date` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`time` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`details` text COLLATE utf8mb4_unicode_ci,
`ip` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`status` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'Complete',
`booked_at` timestamp NULL DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `booking_products` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`booking_id` int(11) NOT NULL,
`product_id` int(11) NOT NULL,
`amount` int(11) NOT NULL,
`price_subtotal` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`price_total` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `booking_services` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`booking_id` int(11) NOT NULL,
`service_id` int(11) NOT NULL,
`reservations` int(11) NOT NULL,
`price_subtotal` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`price_total` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `payment_methods` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `payment_methods_name_unique` (`name`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Here is my query:
return DB::table('bookings')
->selectRaw('payment_methods.name, count(bookings.id) as bookings, (sum(booking_services.price_subtotal) + sum(booking_products.price_subtotal)) as subtotal')
->join('booking_services', 'booking_services.booking_id', '=', 'bookings.id')
->join('booking_products', 'booking_products.booking_id', '=', 'bookings.id')
->join('payment_methods', 'payment_methods.id', '=', 'bookings.payment_method_id')
->where('bookings.status', 'Complete')
->whereBetween('bookings.booked_at', [$this->carbon_from, $this->carbon_to])
->groupBy('payment_methods.id')
->orderBy('payment_methods.name')
->get();
$this->carbon_from and $this->carbon_to are carbon objects which work fine.
I'm trying to obtain the total bookings and a sum of the price_subtotals for each payment method. It seems to be grouping the booking products/services together rather than by each payment method like I want.
Am I missing something here?
Edit: here is the query log:
select payment_methods.name,
count(bookings.id) as bookings,
(sum(booking_services.price_subtotal) + sum(booking_products.price_subtotal)) as subtotal
from `bookings`
inner join `booking_services` on `booking_services`.`booking_id` = `bookings`.`id`
inner join `booking_products` on `booking_products`.`booking_id` = `bookings`.`id`
inner join `payment_methods` on `payment_methods`.`id` = `bookings`.`payment_method_id`
where `bookings`.`status` = ? and `bookings`.`booked_at` between ? and ?
group by `payment_methods`.`id`
order by `payment_methods`.`name` asc

I guess you are getting cross product that is why you are getting wrong numbers for aggregation, what i suggest you, calculate your sum in individual sub clauses and then join these clauses with your main query like
SELECT p.name,
COUNT(DISTINCT b.id) AS bookings,
bs.price_subtotal + bp.price_subtotal AS subtotal
FROM bookings b
INNER JOIN (
SELECT booking_id, SUM(price_subtotal) price_subtotal
FROM booking_services
GROUP BY booking_id
) bs ON b.id = bs.booking_id
INNER JOIN (
SELECT booking_id, SUM(price_subtotal) price_subtotal
FROM booking_products
GROUP BY booking_id
) bp ON b.id = bp.booking_id
INNER JOIN payment_methods p ON p.id = b.payment_method_id
WHERE b.status = ?
AND b.booked_at BETWEEN ? AND ?
GROUP BY p.name
ORDER BY p.name
I have no clue how to transform/write above query using laravel's query builder/eloquent way

Try to group by payment_method_id from the bookings table:
->groupBy('bookings.payment_method_id')

Related

Symfony - doctrine createQueryBuilder multiple Joins

i have this query in MySQL and i want to apply into doctrine
SELECT * FROM ads_list AS al LEFT JOIN (ads_category AS ac, ads_category_main AS acm) ON (ac.id = al.category_id AND ac.parent_cat_id = acm.id)
do you have any idea how to use this with doctrine ?
i'm using this in a Repository
parameter goes to ads_category_main
so i'm trying to select the ads_list with category and each category has a parentCategory which is stored in ads_category_main
SQL
CREATE TABLE `ads_list` (
`id` int(11) NOT NULL,
`category_id` int(11) DEFAULT NULL,
`title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`content` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
`posted_at` date NOT NULL,
`post_xpr` date NOT NULL,
`agency_id` int(11) DEFAULT NULL,
`slug` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `ads_category` (
`id` int(11) NOT NULL,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`parent_cat_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `ads_category_main` (
`id` int(11) NOT NULL,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
so far i got
public function findAllP($main)
{
return $this->createQueryBuilder('pl')
->leftJoin('pl.category', 'al')
->where('al.parentCat = :pc')
->setParameter('pc', $main)
->getQuery()
->execute();
}
and how do i use the output data in a controller ?
As long as you have the entities related you could do the join like
->join/leftJoin('entity.foreign_key','alias')
if the enitities are not related you must specify the "ON" option of the join.
after that you have some ways to handle the result.
$qb->getQuery()->getResult();
$qb->getQuery()->getArrayResult();

inner join query selecting from database where lang is en

Hello i have one query with inner join that take a question and 4 answers from the db. I want to make this query to take only the questions that lang table is 'en'
This is the query:
$mysql->query("SELECT Q.id AS id, Q.question, QA.answer1, QA.answer2,
QA.answer3, QA.answer4, QA.correct, QC.name AS cat_name
FROM question Q
INNER JOIN question_answers QA ON QA.questionFK=Q.id
INNER JOIN question_cats QC ON QC.id=Q.categoryFK
ORDER BY rand()
LIMIT 1");
I try to make it like this:
$mysql->query("SELECT Q.id AS id, Q.question, QA.answer1, QA.answer2,
QA.answer3, QA.answer4, QA.correct, QC.name AS cat_name
FROM question Q
INNER JOIN question_answers QA ON QA.questionFK=Q.id
INNER JOIN question_cats QC ON QC.id=Q.categoryFK
WHERE Q.lang='en'
ORDER BY rand()
LIMIT 1");
But it didn't work, it's select everyting...
Where am i wrong and how should i make it?
Those are the 2 tables:
CREATE TABLE IF NOT EXISTS `question` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`question` varchar(255) NOT NULL,
`cnt` int(10) NOT NULL DEFAULT '0',
`correct` int(10) NOT NULL DEFAULT '0',
`categoryFK` int(3) NOT NULL DEFAULT '0',
`from_userFK` int(10) NOT NULL DEFAULT '0',
`correct_points` int(10) DEFAULT NULL,
`ut` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`del` enum('yes','no') NOT NULL DEFAULT 'no',
`lang` varchar(32) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=26 ;
CREATE TABLE IF NOT EXISTS `question_answers` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`questionFK` int(10) NOT NULL,
`answer1` varchar(255) CHARACTER SET cp1251 NOT NULL,
`answer2` varchar(255) CHARACTER SET cp1251 NOT NULL,
`answer3` varchar(255) CHARACTER SET cp1251 NOT NULL,
`answer4` varchar(255) CHARACTER SET cp1251 NOT NULL,
`correct` int(1) NOT NULL,
`ut` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`del` enum('yes','no') CHARACTER SET cp1251 NOT NULL DEFAULT 'no',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=25 ;
To me it feels like your filer condition WHERE Q.lang='en' doesn't matches any record and so the outcome. Consider trimming it before comparing like
WHERE TRIM(Q.lang) ='en'

Want to populate sets of records from sales table by joining deliveries

I am a novice to MySQL
I have a database where I want to populate sets of records from 'sales' table where condition is sales is not delivered by comparing with 'deliveries' table of 'deliveries.reference_no'.
Both Tables has reference_no in common which is the invoice ref number.
I tried few SQL and got all common filed as it is and tried this one below but its displaying #1052 - Column 'date' in field list is ambiguous.
SELECT sales.id AS sid, date, reference_no, biller_name, customer_name, total_tax, total_tax2, total, internal_note FROM sales LEFT JOIN deliveries ON (sales.reference_no = deliveries.reference_no)
For more information below is my two table schema
Sales
CREATE TABLE IF NOT EXISTS `sales` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`reference_no` varchar(55) NOT NULL,
`warehouse_id` int(11) DEFAULT NULL,
`biller_id` int(11) NOT NULL,
`biller_name` varchar(55) NOT NULL,
`customer_id` int(11) NOT NULL,
`customer_name` varchar(55) NOT NULL,
`date` date NOT NULL,
`note` varchar(1000) DEFAULT NULL,
`internal_note` varchar(1000) DEFAULT NULL,
`inv_total` decimal(25,2) NOT NULL,
`total_tax` decimal(25,2) DEFAULT NULL,
`total` decimal(25,2) NOT NULL,
`invoice_type` int(11) DEFAULT NULL,
`in_type` varchar(55) DEFAULT NULL,
`total_tax2` decimal(25,2) DEFAULT NULL,
`tax_rate2_id` int(11) DEFAULT NULL,
`inv_discount` decimal(25,2) DEFAULT NULL,
`discount_id` int(11) DEFAULT NULL,
`user` varchar(255) DEFAULT NULL,
`updated_by` varchar(255) DEFAULT NULL,
`paid_by` varchar(55) DEFAULT 'cash',
`count` int(11) DEFAULT NULL,
`shipping` decimal(25,2) DEFAULT '0.00',
`pos` tinyint(4) NOT NULL DEFAULT '0',
`paid` decimal(25,2) DEFAULT NULL,
`cc_no` varchar(20) DEFAULT NULL,
`cc_holder` varchar(100) DEFAULT NULL,
`cheque_no` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
Deliveries
CREATE TABLE IF NOT EXISTS `deliveries` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` date NOT NULL,
`time` varchar(10) NOT NULL,
`reference_no` varchar(55) NOT NULL,
`customer` varchar(55) NOT NULL,
`address` varchar(1000) NOT NULL,
`note` varchar(1000) DEFAULT NULL,
`user` varchar(255) DEFAULT NULL,
`updated_by` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
Currently I have a CI program and I am doing like this no Luck what I want to populate
CI PHP
function getdatatableajax()
{
if($this->input->get('search_term')) { $search_term = $this->input->get('search_term'); } else { $search_term = false;}
$this->load->library('datatables');
$this->datatables
->select("sales.id as sid, date, reference_no, biller_name, customer_name, total_tax, total_tax2, total, internal_note")
->from('sales');
$this->datatables->add_column("Actions",
"<center><a href='#' title='$2' class='tip' data-html='true'><i class='icon-folder-close'></i></a> <a href='#' onClick=\"MyWindow=window.open('index.php?module=sales&view=view_invoice&id=$1', 'MyWindow','toolbar=0,location=0,directories=0,status=0,menubar=yes,scrollbars=yes,resizable=yes,width=1000,height=600'); return false;\" title='".$this->lang->line("view_invoice")."' class='tip'><i class='icon-fullscreen'></i></a>
<a href='index.php?module=sales&view=add_delivery&id=$1' title='".$this->lang->line("add_delivery_order")."' class='tip'><i class='icon-road'></i></a>
<a href='index.php?module=sales&view=pdf&id=$1' title='".$this->lang->line("download_pdf")."' class='tip'><i class='icon-file'></i></a>
<a href='index.php?module=sales&view=email_invoice&id=$1' title='".$this->lang->line("email_invoice")."' class='tip'><i class='icon-envelope'></i></a>
</center>", "sid, internal_note")
->unset_column('sid')
->unset_column('internal_note');
echo $this->datatables->generate();
}
Wow, no one answered this yet? Strange.
Your error is because the date field is in both tables. You have to specify which table you want to select date from. So you must write either sales.date or deliveries.date. This goes with the rest of them, once you fix sales.date, your next field reference_no will generate the same error. If you want data from BOTH tables you'll need to assign aliases to them as you did with the first column.
sales.id AS sid, sales.date AS sales_date, deliveries.date AS deliveries_date etc...

Select all threads with tag & the rest of the tags?

I am trying to get all the threads with tag name "test" for example, along with all the other tags. If I add a where clause to my INNER JOIN select then it gets the threads with that tag, but it doesn't get the remaining tags for that thread (each tag is a separate row, I use the group concat to combine them). How can I get the remaining tags also?
I have the following SQL
SELECT `threads`.`id`,
`threads`.`title` AS `title`,
`threads`.`created_at` AS `created_at`,
`threads`.`views` AS `views`,
`threads`.`comments` AS `comments`,
`threads`.`user_id` AS `user_id`,
`tags`
FROM `threads`
INNER JOIN
(SELECT threads_id,
GROUP_CONCAT(DISTINCT `thread_tags`.`thread_tags_title`) AS tags
FROM `thread_tags`
WHERE `thread_tags`.`thread_tags_title` = 'test'
GROUP BY threads_id) thread_tags ON `threads`.`id` = `thread_tags`.`threads_id`
WHERE `threads`.`status` = '1'
ORDER BY `threads`.`views` DESC, `threads`.`created_at` DESC LIMIT 25
OFFSET 0
And the following schemes
Tags
CREATE TABLE `tags` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(200) NOT NULL,
`status` int(11) NOT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
`description` varchar(255) DEFAULT NULL,
`doctors` int(11) DEFAULT NULL,
`threads` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
Thread Tags (table with thread id and tag id)
CREATE TABLE `thread_tags` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`threads_id` int(11) NOT NULL,
`tag_id` int(11) NOT NULL,
`thread_tags_title` varchar(255) NOT NULL DEFAULT '',
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
`description` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
Threads
CREATE TABLE `threads` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`title` varchar(200) NOT NULL,
`body` text NOT NULL,
`status` int(11) NOT NULL,
`views` int(11) NOT NULL,
`rating` int(11) NOT NULL,
`comments` int(11) NOT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
`metadata` text,
PRIMARY KEY (`id`),
KEY `title` (`title`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;
Something like
SELECT threads.*, GROUP_CONCAT(tags.title)
FROM threads AS t
LEFT JOIN thread_tags AS tt ON t.id = tt.threads_id
LEFT JOIN tags AS tt ON tt.tag_id = tags.id
WHERE t.id IN (
SELECT tt.threads_id
FROM thread_tags AS tt
JOIN tags ON tt.tag_id = tags.id
AND tags.title = "test"
)
GROUP BY t.id

Selecting rows that corresponds from other table

I have a product table that stores all products. Also I have a production table that stores productions.
I am using CodeIgniter and datamapper ORM.
Here is tables:
CREATE TABLE IF NOT EXISTS `products` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`kod_stok` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
`kod_lokal` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
`kod_firma` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
`firma` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
`fabrika` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`proje` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
`tanim` mediumtext COLLATE utf8_unicode_ci,
`saatlik_uretim` int(11) NOT NULL,
`status` tinyint(4) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `kod_lokal` (`kod_lokal`),
KEY `kod_firma` (`kod_firma`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;
CREATE TABLE IF NOT EXISTS `productions` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`fabrika` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
`board_no` int(11) NOT NULL,
`date` int(11) DEFAULT NULL, // Unix Timestamp
`operator_id` int(11) DEFAULT NULL,
`product_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `product` (`product_id`),
KEY `date` (`date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;
I am trying to get count of production of given day. But not all products, producting everyday. I need to exlude the products that has 0 count.
$p = new Product();
$p->include_related_count('production');
$p->get();
And I want to add a date interval to production.
Basicly, I want to get all product's production count within a given day.
How can I do that?
Thank you for any advices.
Not sure about codeigniter details, but the following SQL query will generate a production list per day.
To get today's production:
$query = $this->db->query("
SELECT
a.count(*) as produced
, a.product_id
, b.kod_stok as productname
FROM productions a
INNER JOIN products b ON (a.product_id = b.id)
WHERE FROM_UNIXTIME(a.date) = CURDATE()
GROUP BY TO_DAYS(FROM_UNIXTIME(a.date)), a.product_id
");
To get last 7 days production
$query = $this->db->query("
SELECT
a.count(*) as produced
, a.product_id
, b.kod_stok as productname
FROM productions a
INNER JOIN products b ON (a.product_id = b.id)
WHERE FROM_UNIXTIME(a.date)
BETWEEN DATE_SUB(CURDATE(),INTERVAL 7 DAY) AND CURDATE()
GROUP BY TO_DAYS(FROM_UNIXTIME(a.date)), a.product_id
");

Categories