Related
I am having strange results in mysql. I have a view which when executed returns 0 rows but taking the query out of the VIEW and executing it as it is returns 63 rows.
This was working properly but ever since I have migrated the code to AWS RDS I am having this error. I am not able to make any sense why this is happening?
I don't even get any error just the 0 rows being returned.
From here on I am just writing random words because I am not able to post the question as the line of code is more. THis is really annnoying ahh darn I made a typo it must be annoying and not annnoying. My bad I hope you all arent mad at this, I am at my wits end how much more should I type?
I am unable to create a sqlfiddle as the request is too large, I am creating a fiddle like dump if you guys want to recreate it on on your end.
I have also discovered that removing the left join from the VIEW returns results.
The fiddle like dump below returns 3 rows when running the select but the VIEW returns 0 rows
CREATE TABLE `users` (
`id` int(11) NOT NULL,
`user_type` enum('admin','landlord','renter') NOT NULL,
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`email_id` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`company_name` varchar(255) NOT NULL,
`street_address` varchar(255) NOT NULL,
`city` varchar(100) NOT NULL,
`state` varchar(100) NOT NULL,
`zip` varchar(100) NOT NULL,
`phone_no` varchar(50) NOT NULL,
`bank_name` varchar(100) NOT NULL,
`billing_address` varchar(255) NOT NULL,
`name_on_card` varchar(255) NOT NULL,
`bank_account_number` varchar(100) NOT NULL,
`bank_routing_no` varchar(100) NOT NULL,
`has_payzang_account` int(11) NOT NULL DEFAULT '0',
`payzang_api` varchar(255) DEFAULT '',
`payzang_gateway_url` varchar(255) NOT NULL,
`pending_amount` decimal(18,2) DEFAULT NULL COMMENT 'pending key bank(rent deduct) amount for landlord',
`is_active` int(11) NOT NULL COMMENT '0-inactive/1-active',
`is_assigned` int(11) NOT NULL DEFAULT '0' COMMENT 'Is this user assigned to any property',
`assigned_to` int(11) NOT NULL COMMENT 'assigned to which landlord id',
`is_set_up_new_password` enum('0','1') NOT NULL DEFAULT '0' COMMENT 'When admin/landlord adds renter or admin adds landlord an email with id+password is sent to the user. The first time they sign in, it should prompt them to change password page. So this field indicating whether user set up their own password',
`created_at` datetime NOT NULL,
`modified_at` datetime NOT NULL,
`created_by` int(11) NOT NULL,
`modified_by` int(11) NOT NULL,
`is_deleted` int(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `users` (`id`, `user_type`, `first_name`, `last_name`, `email_id`, `password`, `company_name`, `street_address`, `city`, `state`, `zip`, `phone_no`, `bank_name`, `billing_address`, `name_on_card`, `bank_account_number`, `bank_routing_no`, `has_payzang_account`, `payzang_api`, `payzang_gateway_url`, `pending_amount`, `is_active`, `is_assigned`, `assigned_to`, `is_set_up_new_password`, `created_at`, `modified_at`, `created_by`, `modified_by`, `is_deleted`) VALUES
(1, 'admin', 'Super', 'Admin', 'email#email.com', '$2y$10$NwcR0ObYeUM.OfjgMOaajeH6bjjuMTpU2GnF0h623SER37AFk8K9i', 'UIGJ', 'DJ Road', 'Kolkatr', '20', '522236', '(987) 963-9658', '', '', '', '', '', 1, 'xxxxxxxxxxxxxxxxxxxxx', 'https://payzang.transactiongateway.com/api/v2/three-step', '0.00', 1, 0, 0, '1', '2017-06-02 13:56:34', '2017-06-02 13:56:34', 0, 0, 0),
(2, 'landlord', 'Denise', 'Supplee', 'email1#email.com', '$2y$10$PATxnED1/lqr1t7epLJiwu0uVztuXJB97q/XQv7xLc/iCv/m4NK2G', '', '', '', '', '', '2156755615', '', '', '', '', '', 1, 'xxxxxxxxxxxxxxxxxxxxx', 'https://payzang.transactiongateway.com/api/v2/three-step', '230.77', 1, 0, 0, '0', '2017-09-06 16:31:23', '2017-09-10 05:48:45', 0, 1, 0),
(3, 'renter', 'Amanda', 'Scott', 'email2#email.com', '$2y$10$oBU3tbcVT63Asw6CDLCGB.SNAvSpJaOcLBx4NqodFNMwvlSRLc.tO', '', '', '', '', '', '(215) 672-3778', '', '', '', '', '', 0, '', '', NULL, 0, 0, 2, '1', '2017-09-06 17:54:02', '2017-09-06 17:54:02', 2, 0, 1),
(4, 'renter', 'Megan', 'Ridgell', 'email3#email.com', '$2y$10$mPR7U/ri/rpW2n5f6X4mV.6jCycvLTJ/dvttR8seMFSGgRR/tQh1.', '', '', '', '', '', '2154852222', '', '', '', '', '', 0, '', '', NULL, 0, 0, 11, '1', '2017-09-06 18:30:01', '2017-09-12 06:22:23', 0, 2, 1),
(5, 'landlord', 'Brian', 'Davis', 'email4#email.com', '$2y$10$NwcR0ObYeUM.OfjgMOaajeH6bjjuMTpU2GnF0h623SER37AFk8K9i', '', 'PO Box 123', 'Baltimore', '21', '21231', '4104999026', '', '', '', '', '', 0, '', '', '0.00', 1, 0, 0, '0', '2017-09-07 05:19:46', '2017-09-07 05:19:46', 0, 0, 0),
(6, 'renter', 'Greg', 'Davis', 'email5#email.com', '$2y$10$Bd6S4KMGec2NzqKcvsJ1huwmbfACu8oOn56JLFSVQFt6ANg0Vr.ZW', '', '', '', '', '', '4104999026', '', '', '', '', '', 0, '', '', NULL, 1, 0, 5, '1', '2017-09-07 05:30:58', '2017-09-17 04:22:30', 0, 2, 0),
(7, 'renter', 'Maddie', 'Evans', 'email6#email.com', '$2y$10$ry9DBBFJHRhCsV7wpU9s9OI9WQ8BWqCTdk0K9KyYw/VD10mb/yC3O', '', '', '', '', '', '4104999026', '', '', '', '', '', 0, '', '', NULL, 1, 0, 0, '0', '2017-09-07 06:07:10', '2017-09-17 04:14:48', 0, 5, 0),
(8, 'renter', 'Arijita', 'Dey', 'email7#email.com', '$2y$10$NwcR0ObYeUM.OfjgMOaajeH6bjjuMTpU2GnF0h623SER37AFk8K9i', '', '', '', '', '', '3433453535', '', '', '', '', '', 0, '', '', NULL, 0, 0, 2, '0', '2017-09-07 08:08:21', '2017-09-10 05:42:58', 0, 2, 1),
(9, 'renter', 'Arijita', 'Dey', 'email8#email.com', '$2y$10$NwcR0ObYeUM.OfjgMOaajeH6bjjuMTpU2GnF0h623SER37AFk8K9i', '', '', '', '', '', '(564) 564-5645', '', '', '', '', '', 0, '', '', NULL, 0, 0, 5, '0', '2017-09-07 11:05:33', '2017-09-07 11:05:33', 5, 0, 1),
(10, 'renter', 'Rick', 'Roy', 'email9#email.com', '$2y$10$PG/8SkAwOZsSkD.px9FGveFIbriavOTX6vdBWHHmdjQeKgbwr0/wu', '', '', '', '', '', '(533) 453-4534', '', '', '', '', '', 0, '', '', NULL, 0, 0, 5, '0', '2017-09-07 11:11:49', '2017-09-07 11:11:49', 5, 0, 1),
(11, 'landlord', 'Denise', 'Supplee', 'email0#email.com', '$2y$10$52H0QZMFK9ApK7JwaDV9qOlqyOxcyTH3QikQqXgAcLD.zrb.ggoQG', '', '', '', '', '', '2156755615', '', '', '', '', '', 1, 'xxxxxxxxxxxxxxxxxxxxx', 'https://payzang.transactiongateway.com/api/v2/three-step', '0.00', 1, 0, 0, '0', '2017-09-11 14:09:03', '2017-09-18 23:22:57', 0, 1, 0);
CREATE TABLE `lease_with_max_id` (
`id` int(11)
,`lease_id` int(11)
,`renter_id` int(11)
,`landlord_id` int(11)
,`property_id` int(11)
,`is_rent_deducted_form_filled_up` enum('0','1')
,`status` int(11)
,`is_deleted` enum('0','1')
,`is_lease_ended` enum('0','1')
);
CREATE TABLE `properties` (
`id` int(11) NOT NULL,
`user_id` int(11) NOT NULL COMMENT 'landlord''s id',
`property_name` varchar(255) NOT NULL,
`street_address` varchar(255) NOT NULL,
`street_address_2` varchar(255) DEFAULT NULL,
`city` varchar(100) NOT NULL,
`state` varchar(100) NOT NULL,
`zip` varchar(100) NOT NULL,
`rent_amount` decimal(18,2) DEFAULT '0.00',
`is_active` enum('0','1') NOT NULL,
`is_assigned` enum('0','1') NOT NULL DEFAULT '0' COMMENT 'Is assigned to some renter or not',
`created_at` datetime NOT NULL,
`modified_at` datetime NOT NULL,
`is_deleted` int(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `properties` (`id`, `user_id`, `property_name`, `street_address`, `street_address_2`, `city`, `state`, `zip`, `rent_amount`, `is_active`, `is_assigned`, `created_at`, `modified_at`, `is_deleted`) VALUES
(1, 2, 'Test Property', '330 Crooked Billet Rd', '', 'Hatboro', '39', '19040', '0.00', '0', '0', '2017-09-10 05:38:12', '2017-09-10 05:38:12', 0),
(2, 2, '', '515 Main St', 'Apt. 1809', 'Horsham', '39', '19044', '0.00', '1', '1', '2017-10-09 04:24:50', '2017-10-09 04:24:50', 1),
(3, 5, 'Test 1', '123 Brian\'s Test St.', 'Apt. 2', 'Baltimore', '21', '21231', '0.00', '1', '0', '2017-09-07 05:21:10', '2017-09-07 05:21:10', 1),
(4, 5, 'Test 1', '123 Brian\'s Test St.', 'Apt. 2', 'Baltimore', '21', '21231', '0.00', '1', '0', '2017-09-07 05:21:18', '2017-09-07 05:21:18', 1),
(5, 5, 'Test 1', '123 Brian\'s Test St.', 'Apt. 1503', 'Baltimore', '21', '21231', '0.00', '0', '0', '2017-09-11 13:49:11', '2017-09-11 13:49:11', 1),
(6, 5, 'Test 2', '123 Brian\'s Test St.', 'Apt. 1', 'Baltimore', '21', '21231', '0.00', '1', '1', '2017-10-03 04:31:18', '2017-10-03 04:31:18', 0);
CREATE TABLE `geo_states` (
`id` int(11) NOT NULL,
`name` varchar(50) NOT NULL DEFAULT '',
`abv` char(2) NOT NULL DEFAULT '',
`country` char(2) NOT NULL,
`is_state` char(1) DEFAULT NULL,
`is_lower48` char(1) DEFAULT NULL,
`slug` varchar(50) NOT NULL,
`latitude` float(9,6) DEFAULT NULL,
`longitude` float(9,6) DEFAULT NULL,
`population` bigint(20) UNSIGNED DEFAULT NULL,
`area` float(8,2) UNSIGNED DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE VIEW `test` AS SELECT
`users`.`id` AS `id`,
`users`.`user_type` AS `user_type`,
`users`.`first_name` AS `first_name`,
`users`.`last_name` AS `last_name`,
CONCAT(
`users`.`first_name`,
' ',
`users`.`last_name`
) AS `landlord_name`,
`users`.`email_id` AS `email_id`,
`users`.`password` AS `password`,
`users`.`company_name` AS `company_name`,
`users`.`street_address` AS `street_address`,
`users`.`city` AS `city`,
`users`.`state` AS `state`,
`users`.`zip` AS `zip`,
`users`.`phone_no` AS `phone_no`,
`users`.`bank_name` AS `bank_name`,
`users`.`billing_address` AS `billing_address`,
`users`.`name_on_card` AS `name_on_card`,
`users`.`bank_account_number` AS `bank_account_number`,
`users`.`bank_routing_no` AS `bank_routing_no`,
`users`.`has_payzang_account` AS `has_payzang_account`,
(
CASE WHEN(`users`.`has_payzang_account` = 0) THEN 'No' WHEN(`users`.`has_payzang_account` = 1) THEN 'Yes'
END
) AS `has_payzang_account_custom`,
`users`.`payzang_api` AS `payzang_api`,
`users`.`payzang_gateway_url` AS `payzang_gateway_url`,
`users`.`pending_amount` AS `pending_amount`,
`users`.`is_active` AS `is_active`,
(
CASE WHEN(`users`.`is_active` = '0') THEN 'Inactive' WHEN(`users`.`is_active` = '1') THEN 'Active'
END
) AS `is_active_custom`,
`users`.`is_assigned` AS `is_assigned`,
`users`.`assigned_to` AS `assigned_to`,
`users`.`is_set_up_new_password` AS `is_set_up_new_password`,
`users`.`created_at` AS `created_at`,
`users`.`modified_at` AS `modified_at`,
`users`.`created_by` AS `created_by`,
`users`.`modified_by` AS `modified_by`,
`users`.`is_deleted` AS `is_deleted`,
`lease_with_max_id`.`id` AS `lease_with_max_id_id`,
`lease_with_max_id`.`lease_id` AS `lease_with_max_id_lease_id`,
`lease_with_max_id`.`renter_id` AS `lease_with_max_id_renter_id`,
`lease_with_max_id`.`landlord_id` AS `lease_with_max_id_landlord_id`,
`lease_with_max_id`.`property_id` AS `lease_with_max_id_property_id`,
`lease_with_max_id`.`is_rent_deducted_form_filled_up` AS `lease_with_max_id_is_rent_deducted_form_filled_up`,
`lease_with_max_id`.`status` AS `lease_with_max_id_status`,
`lease_with_max_id`.`is_deleted` AS `lease_with_max_id_is_deleted`,
`properties`.`id` AS `property_id`,
`properties`.`user_id` AS `property_user_id`,
`properties`.`property_name` AS `property_property_name`,
`properties`.`street_address` AS `property_street_address`,
`properties`.`street_address_2` AS `property_street_address_2`,
`properties`.`city` AS `property_city`,
`properties`.`state` AS `property_state`,
`properties`.`zip` AS `property_zip`,
`properties`.`rent_amount` AS `property_rent_amount`,
`properties`.`is_active` AS `property_is_active`,
`properties`.`is_assigned` AS `property_is_assigned`,
`properties`.`created_at` AS `property_created_at`,
`properties`.`modified_at` AS `property_modified_at`,
`properties`.`is_deleted` AS `property_is_deleted`,
`geo_states`.`name` AS `geo_states_name`
FROM
(
(
(
`users`
LEFT JOIN `lease_with_max_id` ON
(
(
`lease_with_max_id`.`renter_id` = `users`.`id`
)
)
)
LEFT JOIN `properties` ON
(
(
`properties`.`id` = `lease_with_max_id`.`property_id`
)
)
)
LEFT JOIN `geo_states` ON
(
(
`geo_states`.`id` = `properties`.`state`
)
)
)
WHERE
(
(`users`.`user_type` = 'landlord') AND(`users`.`is_deleted` = 0)
)
ORDER BY
`users`.`id`
DESC
;
I need a sql that uses self join on max id from first table. Please look at the following image of table. I am grouping the table by service_id but I need the last message of each group. So For service group 5 message count should be 3 and last_message should be thirdMsg5. I wrote a sql below everything else is fine but it is throwing an error in the case of self join. It can't recognize msgTbl1.last_message_id. I think I am calling it before preparing it. I need help to solve this problem what would be the best sql to solve this in one query? And if possible please provide me this query in laravel query builder format.
SELECT count(msgTbl1.id) as message_count,
max(msgTbl1.id) as last_message_id,
msgTbl1.body,
msgTbl2.body as last_message,
services.name as service_name
FROM messages msgTbl1
LEFT JOIN (SELECT id, body FROM messages) AS msgTbl2
ON msgTbl2.id = msgTbl1.last_message_id
LEFT JOIN services on services.id = msgTbl1.service_id
WHERE receiver_id = 4 AND read_user = 'no'
GROUP BY msgTbl1.service_id
sql for the message table
CREATE TABLE `messages` (
`id` int(11) UNSIGNED NOT NULL,
`sender_id` int(11) UNSIGNED DEFAULT NULL,
`receiver_id` int(11) UNSIGNED DEFAULT NULL,
`service_id` int(11) UNSIGNED NOT NULL,
`sender_type` enum('user','agent','admin') NOT NULL,
`receiver_type` enum('user','agent','admin') NOT NULL,
`body` text,
`files` varchar(500) DEFAULT NULL COMMENT 'serialize',
`new_notification` enum('no','yes') NOT NULL DEFAULT 'yes',
`read_user` enum('yes','no') NOT NULL DEFAULT 'no',
`read_agent` enum('yes','no') NOT NULL DEFAULT 'no',
`status` enum('active','archive','deleted') NOT NULL DEFAULT 'active',
`created_at` datetime NOT NULL,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `messages` (`id`, `sender_id`, `receiver_id`, `service_id`, `sender_type`, `receiver_type`, `body`, `files`, `new_notification`, `read_user`, `read_agent`, `status`, `created_at`, `updated_at`) VALUES
(1, 22, 4, 5, 'user', 'agent', 'firstMsg5', NULL, 'yes', 'no', 'yes', 'active', '2016-03-24 00:00:00', '2016-04-12 05:40:28'),
(2, 22, 4, 5, 'user', 'agent', 'secondMsg5', NULL, 'yes', 'no', 'yes', 'active', '2016-03-24 00:00:00', '2016-04-12 05:40:31'),
(3, 22, 4, 9, 'user', 'agent', 'firstMsg9', NULL, 'yes', 'yes', 'yes', 'active', '2016-03-24 00:00:00', '2016-04-12 05:40:45'),
(4, 4, 4, 9, 'agent', 'user', 'secondMsg9', NULL, 'yes', 'yes', 'yes', 'active', '2016-03-24 00:00:00', '2016-04-12 05:40:56'),
(5, 22, 4, 5, 'user', 'agent', 'thirdMsg5', NULL, 'yes', 'yes', 'yes', 'active', '2016-03-24 00:00:00', '2016-04-12 05:41:08');
Try this:
SELECT message_count,
last_message_id,
msgTbl1.body,
services.name as service_name
FROM messages msgTbl1
INNER JOIN (
SELECT MAX(id) AS last_message_id, COUNT(*) AS message_count
FROM messages
WHERE read_user = 'no'
GROUP BY service_id) AS msgTbl2
ON msgTbl1.id = msgTbl2.last_message_id
LEFT JOIN services on services.id = msgTbl1.service_id
WHERE receiver_id = 4
I need to make some updates in a table and I want to update the some values based on a like statement. e.g Like '%0010030 a.jpg'
The 0010030 is an example of a product code in my database and it reaches a specific number till now.
I would like to make an for loop to make the changes of i=0010030 to the number I need, i++.
I not sure is it works but i am trying to set it like this '%Variable_Name a.jpg'
In order do it automatically.
I actually have a list of sku/ids that i need to check
Any help or ideas?
What i have:
Insert Ignore Into prefix_virtuemart_product_medias(virtuemart_product_id,virtuemart_media_id,ordering) VALUES (
(Select nprefix_virtuemart_products.virtuemart_product_id from prefix_virtuemart_products where prefix_virtuemart_products.product_sku LIKE '0010030'),
(Select prefix_virtuemart_medias.virtuemart_media_id from prefix_virtuemart_medias
where prefix_virtuemart_medias.file_url LIKE '%0010030 a.jpg' or prefix_virtuemart_medias.file_url LIKE '%0010030 A.jpg'),5);
But i need to automatically change the LIKE parameter from a column that has all of these product codes (it's the product_sku field)
Tables dump with sample data:
CREATE TABLE IF NOT EXISTS `prefix_virtuemart_product_medias` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`virtuemart_product_id` int(1) unsigned NOT NULL DEFAULT '0',
`virtuemart_media_id` int(1) unsigned NOT NULL DEFAULT '0',
`ordering` int(2) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `i_virtuemart_product_id` (`virtuemart_product_id`,`virtuemart_media_id`),
KEY `i_ordering` (`ordering`)
) ENGINE=MyISAM AUTO_INCREMENT=3443 DEFAULT CHARSET=utf8;
-- Dumping data for table virtuemart_product_medias: 1.496 rows
/*!40000 ALTER TABLE `prefix_virtuemart_product_medias` DISABLE KEYS */;
INSERT INTO `prefix_virtuemart_product_medias` (`id`, `virtuemart_product_id`, `virtuemart_media_id`, `ordering`) VALUES
(1909, 2849, 12595, 1),
(1910, 2849, 12596, 1),
(1911, 2849, 12597, 1),
-- Dumping structure for table virtuemart_medias
CREATE TABLE IF NOT EXISTS `prefix_virtuemart_medias` (
`virtuemart_media_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`virtuemart_vendor_id` smallint(1) NOT NULL DEFAULT '1',
`file_title` char(126) NOT NULL DEFAULT '',
`file_description` char(254) NOT NULL DEFAULT '',
`file_meta` char(254) NOT NULL DEFAULT '',
`file_mimetype` char(64) NOT NULL DEFAULT '',
`file_type` char(32) NOT NULL DEFAULT '',
`file_url` varchar(900) NOT NULL DEFAULT '',
`file_url_thumb` varchar(900) NOT NULL DEFAULT '',
`file_is_product_image` tinyint(1) NOT NULL DEFAULT '0',
`file_is_downloadable` tinyint(1) NOT NULL DEFAULT '0',
`file_is_forSale` tinyint(1) NOT NULL DEFAULT '0',
`file_params` varchar(17500) DEFAULT NULL,
`file_lang` varchar(500) NOT NULL,
`shared` tinyint(1) NOT NULL DEFAULT '0',
`published` tinyint(1) NOT NULL DEFAULT '1',
`created_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` int(11) NOT NULL DEFAULT '0',
`modified_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`modified_by` int(11) NOT NULL DEFAULT '0',
`locked_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`locked_by` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`virtuemart_media_id`),
KEY `i_virtuemart_vendor_id` (`virtuemart_vendor_id`),
KEY `i_published` (`published`),
KEY `i_shared` (`shared`)
) ENGINE=MyISAM AUTO_INCREMENT=16811 DEFAULT CHARSET=utf8 COMMENT='Additional Images and Files which are assigned to products';
-- Dumping data for table virtuemart_medias: 4.216 rows
/*!40000 ALTER TABLE `prefix_virtuemart_medias` DISABLE KEYS */;
INSERT INTO `prefix_virtuemart_medias` (`virtuemart_media_id`, `virtuemart_vendor_id`, `file_title`, `file_description`, `file_meta`, `file_mimetype`, `file_type`, `file_url`, `file_url_thumb`, `file_is_product_image`, `file_is_downloadable`, `file_is_forSale`, `file_params`, `file_lang`, `shared`, `published`, `created_on`, `created_by`, `modified_on`, `modified_by`, `locked_on`, `locked_by`) VALUES
(12595, 1, '0010030 MIZA DAIHATSU', '0010030 MIZA DAIHATSU', '0010030 MIZA DAIHATSU', 'image/jpeg', 'product', 'images/stories/virtuemart/product/0010030 a.jpg', '', 0, 0, 0, '', '', 0, 1, '2014-05-10 06:06:44', 466, '2014-05-23 14:09:03', 466, '0000-00-00 00:00:00', 0),
(12596, 1, '0010030 MIZA DAIHATSU', '0010030 MIZA DAIHATSU', '0010030 MIZA DAIHATSU', 'image/jpeg', 'product', 'images/stories/virtuemart/product/0010030 b.jpg', '', 0, 0, 0, '', '', 0, 1, '2014-05-10 06:06:44', 466, '2014-05-23 14:09:03', 466, '0000-00-00 00:00:00', 0),
(12597, 1, '0010030 MIZA DAIHATSU', '0010030 MIZA DAIHATSU', '0010030 MIZA DAIHATSU', 'image/jpeg', 'product', 'images/stories/virtuemart/product/0010030 c.jpg', '', 0, 0, 0, '', '', 0, 1, '2014-05-10 06:06:44', 466, '2014-05-23 14:09:03', 466, '0000-00-00 00:00:00', 0),
(12598, 1, '0010030 MIZA DAIHATSU', '0010030 MIZA DAIHATSU', '0010030 MIZA DAIHATSU', 'image/jpeg', 'product', 'images/stories/virtuemart/product/0010030 d.jpg', '', 0, 0, 0, '', '', 0, 1, '2014-05-10 06:06:44', 466, '2014-05-23 14:09:03', 466, '0000-00-00 00:00:00', 0);
-- Dumping structure for table prefix_virtuemart_products
CREATE TABLE IF NOT EXISTS `prefix_virtuemart_products` (
`virtuemart_product_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`virtuemart_vendor_id` smallint(1) unsigned NOT NULL DEFAULT '1',
`product_parent_id` int(1) unsigned NOT NULL DEFAULT '0',
`product_sku` char(64) DEFAULT NULL,
`product_gtin` char(64) DEFAULT NULL,
`product_mpn` char(64) DEFAULT NULL,
`product_weight` decimal(10,4) DEFAULT NULL,
`product_weight_uom` char(7) DEFAULT NULL,
`product_length` decimal(10,4) DEFAULT NULL,
`product_width` decimal(10,4) DEFAULT NULL,
`product_height` decimal(10,4) DEFAULT NULL,
`product_lwh_uom` char(7) DEFAULT NULL,
`product_url` char(255) DEFAULT NULL,
`product_in_stock` int(1) NOT NULL DEFAULT '0',
`product_ordered` int(1) NOT NULL DEFAULT '0',
`low_stock_notification` int(1) unsigned NOT NULL DEFAULT '0',
`product_available_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`product_availability` char(32) DEFAULT NULL,
`product_special` tinyint(1) DEFAULT NULL,
`product_sales` int(1) unsigned NOT NULL DEFAULT '0',
`product_unit` varchar(8) DEFAULT NULL,
`product_packaging` decimal(8,4) unsigned DEFAULT NULL,
`product_params` varchar(2000) DEFAULT NULL,
`hits` int(11) unsigned DEFAULT NULL,
`intnotes` varchar(18000) DEFAULT NULL,
`metarobot` varchar(400) DEFAULT NULL,
`metaauthor` varchar(400) DEFAULT NULL,
`layout` char(16) DEFAULT NULL,
`published` tinyint(1) DEFAULT NULL,
`pordering` mediumint(2) unsigned NOT NULL DEFAULT '0',
`created_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` int(11) NOT NULL DEFAULT '0',
`modified_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`modified_by` int(11) NOT NULL DEFAULT '0',
`locked_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`locked_by` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`virtuemart_product_id`),
KEY `idx_product_virtuemart_vendor_id` (`virtuemart_vendor_id`),
KEY `idx_product_product_parent_id` (`product_parent_id`),
KEY `i_product_special` (`product_special`),
KEY `i_published` (`published`),
KEY `i_pordering` (`pordering`)
) ENGINE=MyISAM AUTO_INCREMENT=3809 DEFAULT CHARSET=utf8 COMMENT='All products are stored here.';
-- Dumping data for table kontosdb.prefix_virtuemart_products: 960 rows
/*!40000 ALTER TABLE `prefix_virtuemart_products` DISABLE KEYS */;
INSERT INTO `prefix_virtuemart_products` (`virtuemart_product_id`, `virtuemart_vendor_id`, `product_parent_id`, `product_sku`, `product_gtin`, `product_mpn`, `product_weight`, `product_weight_uom`, `product_length`, `product_width`, `product_height`, `product_lwh_uom`, `product_url`, `product_in_stock`, `product_ordered`, `low_stock_notification`, `product_available_date`, `product_availability`, `product_special`, `product_sales`, `product_unit`, `product_packaging`, `product_params`, `hits`, `intnotes`, `metarobot`, `metaauthor`, `layout`, `published`, `pordering`, `created_on`, `created_by`, `modified_on`, `modified_by`, `locked_on`, `locked_by`) VALUES
(2849, 1, 0, '0100300', '', '', 0.0000, 'KG', 0.0000, 0.0000, 0.0000, 'CM', '', 0, 0, 0, '2010-05-17 00:00:00', '', 0, 0, 'KG', NULL, 'min_order_level=""|max_order_level=""|step_order_level=""|product_box=""|', NULL, '', '', '', '0', 1, 0, '2010-05-17 12:46:52', 466, '2014-05-16 06:35:16', 466, '0000-00-00 00:00:00', 0),
(2850, 1, 0, '0100623', NULL, NULL, 0.0000, 'KG', 0.0000, 0.0000, 0.0000, 'CM', '', 0, 0, 0, '2010-05-28 00:00:00', '', 0, 0, 'Τεμάχιο', NULL, 'min_order_level=null|max_order_level=null|step_order_level=null|product_box=null|', NULL, '', '', '', '', 1, 0, '2010-05-28 12:40:19', 466, '2014-05-10 06:06:44', 466, '0000-00-00 00:00:00', 0),
(2851, 1, 0, '0100630', NULL, NULL, 0.0000, 'KG', 0.0000, 0.0000, 0.0000, 'CM', '', 0, 0, 0, '2010-05-28 00:00:00', '', 0, 0, 'Τεμάχιο', NULL, 'min_order_level=null|max_order_level=null|step_order_level=null|product_box=null|', NULL, '', '', '', '', 1, 0, '2010-05-28 12:47:16', 466, '2014-05-10 06:06:44', 466, '0000-00-00 00:00:00', 0);
I'm not sure I understand what you are having trouble with. With the following give you the results you expect?
SELECT
nprefix_virtuemart_products.virtuemart_product_id,
pvm.virtuemart_media_id,
5
FROM
prefix_virtuemart_products pvp
INNER JOIN prefix_virtuemart_medias pvm ON pvm.file_url LIKE CONCAT('%', pvp.product_sku, ' a.jpg')
OR pvm.file_url LIKE CONCAT('%', pvp.product_sku, ' A.jpg');
For this to work the subquery can only return one row, I don't know what your data looks like, so that might not be the case.
CREATE TABLE IF NOT EXISTS `messages` (
`id` int(11) unsigned NOT NULL auto_increment,
`user_id` int(11) unsigned NOT NULL,
`node_id` int(11) unsigned NOT NULL,
`reciever_id` int(11) unsigned NOT NULL,
`created` datetime default NULL,
`modified` datetime default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ;
INSERT INTO `messages` (`id`, `user_id`, `node_id`, `reciever_id`, `created`, `modified`) VALUES
(1, 1, 1, 15, '2011-12-07 00:00:00', '2011-12-07 02:00:00'),
(2, 15, 1, 1, '2011-12-07 02:00:00', '2011-12-07 02:00:00'),
(3, 15, 2, 1, '2011-12-07 11:00:00', '2011-12-07 11:00:00'),
(4, 1, 2, 15, '2011-12-07 11:00:00', '2011-12-07 11:00:00'),
(5, 1, 3, 18, '2011-12-07 11:00:00', '2011-12-07 11:00:00'),
(6, 18, 3, 1, '2011-12-07 11:00:00', '2011-12-07 11:00:00'),
(7, 1, 4, 18, '2011-12-07 12:00:00', '2011-12-07 12:00:00'),
(8, 18, 4, 1, '2011-12-07 12:00:00', '2011-12-07 12:00:00');
CREATE TABLE IF NOT EXISTS `nodes` (
`id` int(11) unsigned NOT NULL auto_increment,
`message` text NOT NULL,
`author_id` int(11) unsigned NOT NULL,
`read` tinyint(1) default NULL,
`created` datetime default NULL,
`modified` datetime default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
INSERT INTO `nodes` (`id`, `message`, `author_id`, `read`, `created`, `modified`) VALUES
(1, 'Hi! How are you ? dude wanna meet up this weekend ?', 1, 0, '2011-12-07 02:00:00', '2011-12-07 02:00:00'),
(2, 'Sure. wanna go to Mangalore Pearl to eat Neer Dosa..', 15, 0, '2011-12-07 11:00:00', '2011-12-07 11:00:00'),
(3, 'Hi How are u Buddy ? Long time no see...', 1, 0, '2011-12-07 11:00:00', '2011-12-07 11:00:00'),
(4, 'yeah. are you back in town ? i think we should meet up man. its been ages ....', 18, 0, '2011-12-07 12:00:00', '2011-12-07 12:00:00');
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) unsigned NOT NULL auto_increment,
`first_name` varchar(255) default NULL,
`last_name` varchar(255) default NULL,
`email` varchar(255) default NULL,
`password` varchar(40) default NULL,
`username` varchar(255) default NULL,
`birthday` date default NULL,
`gender` varchar(255) default NULL,
`city_id` int(11) unsigned NOT NULL,
`status` varchar(255) NOT NULL,
`created` datetime default NULL,
`modified` datetime default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=21 ;
I am trying to create a subquery in Cake. but not sure how to start :(
This is the SQL i want to execute
SELECT *
FROM (
SELECT *
FROM messages AS msg
WHERE user_id =1
ORDER BY modified DESC
) AS latest_message
GROUP BY reciever_id
Is it better to use sub Queries or write SQL statement ?
I'm not really sure what the necessity for a subquery is here, wouldn't something like this do the trick?
$this->Message->find('all', array(
'conditions' => array('Message.user_id' => 1),
'order' => array('Message.modified' => 'DESC'),
'group' => array('Message.receiver_id')
));
This would retrieve all the messages from the user with id 1, ordered by date modified and grouped by receiver_id.
Since you are trying to group by the receiver, then why not alter the query to retrieve the receivers and then the messages that belong to each one? Below, I am assuming use of the containable behavior.
$this->Receiver->find('all', array(
'contain' => array(
'Message' => array(
'conditions' => array('Message.user_id' => 1),
'order' => array('Message.modified' => 'DESC'),
)
)
));
EDIT
I added this query to see if it helps based on your comment.
$this->Message->find(
'all',
array(
'conditions' => array('Message.user_id' => 1),
'fields' => array('Message.*', 'MAX(Message.modified) as max_mod'),
'group' => 'Message.receiver_id'
)
);
CREATE TABLE IF NOT EXISTS `tweets_comment_tbl` (
`tweet_comment_id` int(12) NOT NULL AUTO_INCREMENT,
`tweet_id` int(12) NOT NULL,
`tweets_comment` text NOT NULL,
`created` int(12) NOT NULL,
`changed` int(12) NOT NULL,
`uid` int(12) NOT NULL,
`userip` varchar(20) NOT NULL,
`referer` text NOT NULL,
`status` int(2) NOT NULL DEFAULT '1',
PRIMARY KEY (`tweet_comment_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
--
-- Dumping data for table `tweets_comment_tbl`
--
INSERT INTO `tweets_comment_tbl` (`tweet_comment_id`, `tweet_id`, `tweets_comment`, `created`, `changed`, `uid`, `userip`, `referer`, `status`) VALUES
(1, 1, 'COMMENT USER1', 1319395671, 1319395671, 3, '127.0.0.1', 'http://localhost/drupal_tutorial/', 1),
(2, 2, 'comment admin user', 1319395724, 1319395724, 1, '127.0.0.1', 'http://localhost/drupal_tutorial/node', 1),
(3, 2, 'USER COMMENTING HIS COMMENT', 1319395838, 1319395838, 3, '127.0.0.1', 'http://localhost/drupal_tutorial/', 1),
(4, 2, 'ADMIN COMMENTING FOR HIS COMMENT', 1319395865, 1319395865, 1, '127.0.0.1', 'http://localhost/drupal_tutorial/node', 1),
(5, 2, 'dddCOMMENT USER1: ADMIN DOING COMMENT FOR STATUS UPDATE1', 1319395905, 1319395905, 1, '127.0.0.1', 'http://localhost/drupal_tutorial/node', 1);
my second table
CREATE TABLE IF NOT EXISTS `tweets_tbl` (
`tweet_id` int(11) NOT NULL AUTO_INCREMENT,
`tweets` text NOT NULL,
`created` int(12) NOT NULL,
`changed` int(12) NOT NULL,
`uid` int(12) NOT NULL,
`userip` varchar(20) NOT NULL,
`referer` text NOT NULL,
`status` int(2) NOT NULL DEFAULT '1',
PRIMARY KEY (`tweet_id`),
KEY `tweet_id` (`tweet_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `tweets_tbl`
--
INSERT INTO `tweets_tbl` (`tweet_id`, `tweets`, `created`, `changed`, `uid`, `userip`, `referer`, `status`) VALUES
(1, 'STATUS UPDATE 1', 1319395633, 1319395633, 1, '127.0.0.1', 'http://localhost/drupal_tutorial/node', 1),
(2, 'Status update user1', 1319395696, 1319395696, 3, '127.0.0.1', 'http://localhost/drupal_tutorial/node', 1);
Trying join query
SELECT ut.picture as picture, tct.tweet_id as tweet_id, tct.tweets_comment as tweets_comment, tct.changed
FROM tweets_comment_tbl tct
RIGHT JOIN users as ut ON tct.uid=ut.uid AND tct.tweet_id=2 AND tct.status = 1
order by tct.created desc
return records for above query
picture tweet_id tweets_comment changed
1 COMMENT USER1 1319395671
NULL NULL NULL
picture-1.png NULL NULL NULL
actually what i expected is
picture tweet_id tweets_comment changed
1 COMMENT USER1 1319395671
Why query has been returning NULL records, i am not sure where i made mistake in join query.
The query returns all rows from users and joins tweets_comment_tbl based on condition in ON. If no records in tweets_comment_tbl matches userid, the record from users still included into recordset. You probably need INNER JOIN if you want to see just users with comments.
Side note :
I think it's almost always possible to avoid RIGHT JOIN; queries with LEFT JOIN are much easier to read and understand.