trouble creating update query using 3 tables mysql - mysql

I Have closing balances in Akey table and i have pool id in cpp table i want to update value of Bppd key_amount column with akey closing_balance but before
that i have to check the default_pool='ACTIVE' from table cpp
sample tables are mentioned
TABLE Akey (
`id` int(11) NOT NULL,
`serial_key` varchar(82) DEFAULT NULL,
`closing_balance` double(20,2) DEFAULT '0.00',
`product_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
)
TABLE Bppd(
`id` int(11) NOT NULL AUTO_INCREMENT,
`pool_id` int(11) DEFAULT NULL,
`product_id` int(11) DEFAULT NULL,
`akey_id` int(11) DEFAULT NULL,
`key_amount` double DEFAULT '0',
`working_balance` double DEFAULT '0',
PRIMARY KEY (`id`)
)
TABLE Cpp (
`id` int(11) NOT NULL AUTO_INCREMENT,
`akey_id` int(11) DEFAULT NULL,
`pool_id` int(11) DEFAULT NULL,
`default_pool` varchar(15) DEFAULT NULL,
PRIMARY KEY (`id`)
)
I have Following query
UPDATE `Bppd` ppd
JOIN AKey kk ON ppd.`akey_id` = kk.`id`
JOIN `Cpp` pp ON pp.`akey_id` = kk.`id` AND pp.`default_pool` = 'ACTIVE' AND ppd.`akey_id` = pp.`akey_id`
SET ppd.`key_amount`= kk.`closing_balance`

Try this
UPDATE Bppd b INNER JOIN Akey a on a.id = b.akey_id AND a.product_id = b.product_id
INNER JOIN Cpp c ON b.key_id = c.akey_id AND b.pool_id = c.pool_id
SET b.key_amount = a.closing_balance
WHERE c.default_pool='ACTIVE'
Or this:
UPDATE Bppd b SET key_amount = (SELECT closing_balance FROM Akey a WHERE a.id = b.akey_id and a.product_id = b.product_id)
WHERE EXISTS (
SELECT 1 FROM Cpp c
WHERE b.akey_id = c.akey_id AND b.pool_id = c.pool_id
AND c.default_pool='ACTIVE'
)
AND EXISTS (
SELECT 1 FROM Akey a
WHERE a.id = b.akey_id and a.product_id = b.product_id
)

Related

SQL JOIN having a result of table rows to columns

I'm trying to connect two tables and show the matching rows as columns.
ps_product table contains the values of the product, ps_image_shop contains the image values and id's linking to the product. What I would like to have is, showing matching rows as column at SQL result below.
Table 1 (ps_product) 1
Table 2 (ps_image_shop) 2
CURRENT SQL QUERY
SELECT SQL_CALC_FOUND_ROWS p.`id_product` AS `id_product`,
p.`reference` AS `reference`,
sa.`price` AS `price`,
p.`id_shop_default` AS `id_shop_default`,
p.`is_virtual` AS `is_virtual`,
pl.`name` AS `name`,
pl.`link_rewrite` AS `link_rewrite`,
sa.`active` AS `active`,
shop.`name` AS `shopname`,
image_shop.`id_image` AS `id_image`,
cl.`name` AS `name_category`,
0 AS `price_final`,
pd.`nb_downloadable` AS `nb_downloadable`,
sav.`quantity` AS `sav_quantity`,
IF(sav.`quantity`<=0, 1, 0) AS `badge_danger`
FROM `ps_product` p
LEFT JOIN `ps_product_lang` pl ON (pl.`id_product` = p.`id_product` AND pl.`id_lang` = 1 AND pl.`id_shop` = 1)
LEFT JOIN `ps_stock_available` sav ON (sav.`id_product` = p.`id_product` AND sav.`id_product_attribute` = 0 AND sav.id_shop = 1 AND sav.id_shop_group = 0 )
JOIN `ps_product_shop` sa ON (p.`id_product` = sa.`id_product` AND sa.id_shop = 1)
LEFT JOIN `ps_category_lang` cl ON (sa.`id_category_default` = cl.`id_category` AND cl.`id_lang` = 1 AND cl.id_shop = 1)
LEFT JOIN `ps_category` c ON (c.`id_category` = cl.`id_category`)
LEFT JOIN `ps_shop` shop ON (shop.id_shop = 1)
LEFT JOIN `ps_image_shop` image_shop ON (image_shop.`id_product` = p.`id_product` AND image_shop.id_shop = 1)
LEFT JOIN `ps_image` i ON (i.`id_image` = image_shop.`id_image`)
LEFT JOIN `ps_product_download` pd ON (pd.`id_product` = p.`id_product`)
WHERE (1 AND state = 1)
ORDER BY `id_product` desc
LIMIT 0, 100;
CREATE QUERIES
CREATE TABLE IF NOT EXISTS `ps_product` (
`id_product` int(10) unsigned NOT NULL,
`id_supplier` int(10) unsigned DEFAULT NULL,
`id_manufacturer` int(10) unsigned DEFAULT NULL,
`id_category_default` int(10) unsigned DEFAULT NULL,
`id_shop_default` int(10) unsigned NOT NULL DEFAULT '1',
`id_tax_rules_group` int(11) unsigned NOT NULL,
`on_sale` tinyint(1) unsigned NOT NULL DEFAULT '0',
`online_only` tinyint(1) unsigned NOT NULL DEFAULT '0',
`ean13` varchar(13) DEFAULT NULL,
`isbn` varchar(32) DEFAULT NULL,
`upc` varchar(12) DEFAULT NULL,
`ecotax` decimal(17,6) NOT NULL DEFAULT '0.000000',
`quantity` int(10) NOT NULL DEFAULT '0',
) ENGINE=InnoDB AUTO_INCREMENT=138 DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `ps_image_shop` (
`id_product` int(10) unsigned NOT NULL,
`id_image` int(11) unsigned NOT NULL,
`id_shop` int(11) unsigned NOT NULL,
`cover` tinyint(1) unsigned DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

WHERE in a triple INNER JOIN

I'm trying to find all the values where empresas.id = (a number);
Here's my query:
SELECT empresas.nome, departamentos.nomeDepartamento,
funcionarios.nomeFuncionario, funcionarios.email, funcionarios.idFuncionario FROM empresas
INNER JOIN departamentos ON empresas.id = departamentos.idEmpresas
INNER JOIN funcionarios ON departamentos.id = funcionarios.idDepartamentos
ORDER BY empresas.nome ASC;
I've tried to put WHERE in the end, right after ASC :
...ORDER BY empresas.nome ASC WHERE id.empresas = 2;
And none of my tries have worked.
Tables:
CREATE TABLE `departamentos` (
`id` int(11) NOT NULL,
`idEmpresas` int(11) DEFAULT NULL,
`nomeDepartamento` varchar(30) DEFAULT NULL
)
CREATE TABLE `empresas` (
`id` int(11) NOT NULL,
`nome` varchar(30) DEFAULT NULL
)
CREATE TABLE `funcionarios` (
`idFuncionarios` int(11) NOT NULL,
`idDepartamentos` int(11) DEFAULT NULL,
`nomeFuncionario` varchar(30) DEFAULT NULL,
`email` varchar(30) DEFAULT NULL,
`senha` varchar(30) DEFAULT NULL
)
What i wanna do is JOIN those table to return empresas.nome, departamentos.nomeDepartamento,
funcionarios.nomeFuncionario, funcionarios.email, funcionarios.idFuncionario and then select then by empresas.
Thank you for your help!
SELECT empresas.nome, departamentos.nomeDepartamento,
funcionarios.nomeFuncionario, funcionarios.email, funcionarios.idFuncionario FROM empresas
INNER JOIN departamentos ON empresas.id = departamentos.idEmpresas
INNER JOIN funcionarios ON departamentos.id = funcionarios.idDepartamentos
WHERE empreas.id = 2 /* <=== The line to add */
ORDER BY empresas.nome ASC;

Calculate Sum that cross 3 tables in MYSQL

I have 3 tables (trx_batch, trx_doc_trx_item).
I want user to enter id(trx_batch). Then the system will auto calculate the sum of trx_item which linked to trx_doc, and trx_doc is linked to trx_batch.
My table:
CREATE TABLE IF NOT EXISTS `trx_batch` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`batchDate` date NOT NULL,
`status` varchar(1) NOT NULL
PRIMARY KEY (`id`)
);
CREATE TABLE IF NOT EXISTS `trx_doc` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`idBatch` int(11) NOT NULL,
`subject` varchar(200) DEFAULT NULL,
`status` varchar(1) NOT NULL DEFAULT 'A',
PRIMARY KEY (`id`)
);
CREATE TABLE IF NOT EXISTS `trx_item` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`idDoc` int(11) NOT NULL,
`amount` decimal(14,4) NOT NULL,
`amtTax` decimal(12,4) DEFAULT NULL,
PRIMARY KEY (`id`)
);
Sample data that I have:
trx_batch
id: 1
trx_doc
id:1
idBatch: 1
trx_item:
id:1
idDoc:1
amount: 500
amtTax : 30
id:2
idDoc:1
amount:100
amtTax:20
If user enter 1 as id for trx_batch. The total should be 650.00.
How to write in mysql format? Please help me thanks!
SELECT
SUM( COALESCE( i.amount, 0 ) + COALESCE( i.amtTax, 0 ) ) AS SumAmountAndTax
FROM
trx_item AS i
INNER JOIN trx_doc AS d ON i.idDoc = doc.id
INNER JOIN trx_batch AS b ON d.idBatch = b.id
WHERE
b.id = :batchIdParameterValue

my join returns 0 results

I have to following 3 tables: room, reservation and reservationroom
Their structure is as follows:
CREATE TABLE `room` (
`roomID` int(11) NOT NULL AUTO_INCREMENT,
`hotelID` int(11) NOT NULL,
`roomtypeID` int(11) NOT NULL,
`roomNumber` int(11) NOT NULL,
`roomName` varchar(255) NOT NULL,
`roomName_en` varchar(255) NOT NULL,
`roomDescription` text,
`roomDescription_en` text,
`roomSorder` int(11) NOT NULL,
`roomVisible` tinyint(4) NOT NULL,
PRIMARY KEY (`roomID`)
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8;
CREATE TABLE `reservation` (
`reservationID` int(11) NOT NULL AUTO_INCREMENT,
`customerID` int(11) NOT NULL,
`hotelID` int(11) NOT NULL,
`reservationCreatedOn` datetime NOT NULL,
`reservationCreatedFromIp` varchar(255) CHARACTER SET greek NOT NULL,
`reservationNumberOfAdults` tinyint(4) NOT NULL,
`reservationNumberOfChildrens` tinyint(4) NOT NULL,
`reservationArrivalDate` date NOT NULL,
`reservationDepartureDate` date NOT NULL,
`reservationCustomerComment` text CHARACTER SET greek,
PRIMARY KEY (`reservationID`)
) ENGINE=InnoDB AUTO_INCREMENT=46 DEFAULT CHARSET=utf8;
CREATE TABLE `reservationroom` (
`reservationroomID` int(11) NOT NULL AUTO_INCREMENT,
`reservationID` int(11) NOT NULL,
`hotelID` int(11) NOT NULL,
`roomID` int(11) NOT NULL,
PRIMARY KEY (`reservationroomID`)
) ENGINE=InnoDB AUTO_INCREMENT=46 DEFAULT CHARSET=utf8;
(please note that foreign keys have been removed from create statements for sake of simplicity)
What I am trying to do: I want to get all rooms that are not reserved for specific dates, that is only the free rooms from the specific hotel (I have its ID)
Here is the query that I have right now:
SELECT r.* FROM room r
LEFT JOIN `reservationroom` rr
ON r.`hotelID` = rr.`hotelID`
AND r.`roomID` = rr.`roomID`
LEFT JOIN `reservation` re
ON rr.`reservationID` = re.`reservationID`
WHERE (rr.`reservationroomID` = ''
OR rr.`reservationroomID` IS NULL
AND re.`reservationArrivalDate` >= 2014-08-27
AND re.`reservationDepartureDate` <= 2014-08-29
AND r.`hotelID` = 10
AND r.`roomVisible` = 1);
This query now returns 0 results. It should return 9 records, since the hotel with ID = 10 has 9 rooms that are free (no resevations for specific dates exist in the reservation table)
Can anyone give me a hand with this please? I am trying to sort this out couple of hours, without any success.
You are using left join, so conditions on all but the first table should be in the on clauses. I think you want a query more like this:
SELECT r.*
FROM room r LEFT JOIN
`reservationroom` rr
ON r.`hotelID` = rr.`hotelID` AND
r.`roomID` = rr.`roomID` LEFT JOIN
`reservation` re
ON rr.`reservationID` = re.`reservationID` AND
re.`reservationArrivalDate` >= 2014-08-27 AND
re.`reservationDepartureDate` <= 2014-08-29
WHERE r.`hotelID` = 10 AND r.`roomVisible` = 1 AND re.reservationID is null;
I'm not sure what the comparison is to the empty string. It doesn't seem necessary for this purpose.

Yii CDbCriteria

Hollow i have kind of query for my table select m.voterID, sum(jm.mark) from marks m
left join marks jm on jm.id = m.id
where jm.voterID in (1,2)
group by m.voterID
and i don't understand how to wright it using the CDbCriteria.
table structute is
`id` int(11) NOT NULL AUTO_INCREMENT,
`voterId` int(11) NOT NULL,
`votedId` int(11) NOT NULL,
`mark` int(11) NOT NULL,
`creation_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
$criteria = new CDbCriteria();
$criteria->select = 'm.voterID, sum(jm.mark)';
$criteria->from = 'marks m';
$criteria->join = 'left join marks jm on jm.id = m.id';
$criteria->condition = 'jm.voterID in (1,2)';
$criteria->group = 'm.voterID';