I have a table:
CREATE TABLE IF NOT EXISTS `ccu_log` (
`id` int(15) NOT NULL AUTO_INCREMENT,
`ccu` int(10) DEFAULT NULL,
`time` time DEFAULT NULL,
`date` date DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5626 ;
--
-- Dumping data for table `ccu_log`
--
INSERT INTO `ccu_log` (`id`, `ccu`, `time`, `date`) VALUES
(1, 0, '22:27:30', '2015-01-08'),
(2, 0, '22:29:01', '2015-01-08'),
(3, 0, '22:30:31', '2015-01-08'),
(4, 0, '22:32:01', '2015-01-08'),
(5, 3, '22:33:31', '2015-01-08'),
(6, 0, '22:35:01', '2015-01-08'),
(7, 4, '22:36:31', '2015-01-08'),
(8, 8, '22:38:01', '2015-01-09'),
(9, 5, '22:39:31', '2015-01-09'),
(10, 1, '22:41:01', '2015-01-09');
When I want select data with mysql query:
SELECT *
FROM `ccu_log`
WHERE UNIX_TIMESTAMP("date") = UNIX_TIMESTAMP('09-01-2015')
It show all rows from my table. If i user query:
SELECT * FROM `ccu_log` where UNIX_TIMESTAMP(date) = UNIX_TIMESTAMP('09-01-2015')
It doesn't return the expected results.
Any suggestions?
When the field is already of type date, you don't even need to convert to a timestamp for comparison. Just do
SELECT * FROM `ccu_log` WHERE `date`='2015-01-09'
Please try like this,
SELECT * FROM `ccu_log` where DATE_FORMAT(date,"%d-%m-%Y") = '09-01-2015';
Related
ROOM TABLE
CREATE TABLE `room` (
`id` int(11) NOT NULL,
`price` double NOT NULL,
`type` varchar(255) NOT NULL,
`photo` varchar(255) NOT NULL,
`max_capacity` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `room` (`id`, `price`, `type`, `photo`, `max_capacity`) VALUES (1, 25, 'Suite', 'something.jpg', 3);
INSERT INTO `room` (`id`, `price`, `type`, `photo`, `max_capacity`) VALUES (2, 20, 'Single', 'something.jpg', 1);
INSERT INTO `room` (`id`, `price`, `type`, `photo`, `max_capacity`) VALUES (3, 250, 'Family Suite', 'something.jpg', 8);
INSERT INTO `room` (`id`, `price`, `type`, `photo`, `max_capacity`) VALUES (4, 20, 'Twin', 'something.jpg', 2);
INSERT INTO `room` (`id`, `price`, `type`, `photo`, `max_capacity`) VALUES (5, 20, 'Twin', 'something.jpg', 2);
INSERT INTO `room` (`id`, `price`, `type`, `photo`, `max_capacity`) VALUES (6, 25, 'Suite', 'something.jpg', 3);
ALTER TABLE `room`
ADD PRIMARY KEY (`id`);
ORDERS TABLE
CREATE TABLE `orders` (
`id` int(11) NOT NULL,
`checkin` date NOT NULL,
`checkout` date NOT NULL,
`id_user` int(11) NOT NULL,
`id_room` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `orders` (`id`, `checkin`, `checkout`, `id_user`, `id_room`) VALUES
(1, '2023-01-12', '2023-01-13', 1, 1),
(2, '2023-01-11', '2023-01-15', 1, 2);
ALTER TABLE `orders`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
COMMIT;
I tried the following:
SELECT id,type FROM room WHERE (SELECT \* FROM orders WHERE (2023-01-12 NOT BETWEEN `checkin` AND `checkout`) AND (2023-01-13 NOT BETWEEN `checkin` AND `checkout`));
I expect to get all the room with id (2-6) and the id = 1 to not be showed if the reservation is made in the interval of 12.01.2023(checkin choosen by client x) and 13.01.2023(checkout choosen by client x).
I would appreciate your help alot! THANK YOU!
Try this
SELECT id, type
FROM room
WHERE id NOT IN (
SELECT id_room
FROM orders
WHERE checkin >= '2023-01-12' AND checkout <= '2023-01-13'
);
I have two query to get count and sum of rate for unique ip's.
Query one groups by date and query two groups by country
This is the table
DROP TABLE IF EXISTS `stats`;
CREATE TABLE IF NOT EXISTS `stats` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` int(5) UNSIGNED NOT NULL,
`country` int(3) UNSIGNED NOT NULL,
`user_ip` int(50) UNSIGNED NOT NULL,
`timestamp` int(10) UNSIGNED NOT NULL,
`rate` int(7) UNSIGNED NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Dumping data for table `stats`
--
INSERT INTO `stats` (`id`, `user_id`, `country`, `user_ip`, `timestamp`, `rate`) VALUES
(1, 1, 1, 1111111111, 1489999983, 15000),
(2, 1, 2, 1111111112, 1489999984, 10000),
(3, 1, 1, 1111111111, 1489999985, 10000),
(4, 1, 1, 1111111111, 1490086333, 10000),
(5, 1, 2, 1111111111, 1490086334, 10000),
(6, 1, 1, 1111111121, 1490086335, 10000);
These are the queries I am using to get data
To get sum of rates based on date I use following query
SELECT COUNT(`user_ip`) AS `count`, SUM(`rate`) AS `rate`, `timestamp`
FROM (
SELECT DISTINCT `user_ip`, `rate`, `timestamp`
FROM `stats`.`stats`
WHERE `user_id`=? `timestamp`>=? AND `timestamp`<=?
GROUP BY DATE(FROM_UNIXTIME(`timestamp`)),`user_ip`
) c
GROUP BY DATE(FROM_UNIXTIME(`timestamp`))
Result
date count rate
20-03-2017 2 25000
21-03-2017 2 20000
To get sum of rates based on country I use following query
SELECT COUNT(`user_ip`) AS `count`, SUM(`rate`) AS `rate`, `timestamp`, `country`
FROM (
SELECT DISTINCT `user_ip`, `rate`, `timestamp`, `country`
FROM `stats`.`stats`
WHERE `user_id`=? `timestamp`>=? AND `timestamp`<=?
GROUP BY DATE(FROM_UNIXTIME(`timestamp`)),`user_ip`
) c
GROUP BY `country`
Result
country count rate
1 3 35000
2 1 10000
Since these two query are nearly same and fetches same rows from table is it possible to get both result from single query instead of two query.
Also please suggest if it can be be done in PHP effectively than MYSQL.
Thanks
Try this in php
$country="";
$groupByCondition="DATE(FROM_UNIXTIME(`timestamp`))";
if(/*BasedOnCountry*/){
$country=", `country`";
$groupByCondition = "`country`";
}
$query= "SELECT COUNT(`user_ip`) AS `count`, SUM(`rate`) AS `rate`, `timestamp`"+ $country +"
FROM (
SELECT DISTINCT `user_ip`, `rate`, `timestamp`"+ $country +"
FROM `stats`.`stats`
WHERE `user_id`=? `timestamp`>=? AND `timestamp`<=?
GROUP BY DATE(FROM_UNIXTIME(`timestamp`)),`user_ip`
) c
GROUP BY "+ $groupByCondition ;
//execute the query and get the results
I have the joy of recreating the phone, by building a customized messaging system in PHP that uses an API to send and receive messages.
I'm trying to emulate the functionality found in Facebook messaging on their desktop site.
[Col 1] [Col 2]
A list of the Conversation View.
latest messages
received in order
of Newest to oldest
I am having issues with the Query for the first column.
I currently have a table in MySQL with the following structure:
CREATE TABLE IF NOT EXISTS `History` (
`ID` int(10) NOT NULL AUTO_INCREMENT COMMENT 'MessageID',
`Sender` varchar(10) NOT NULL,
`Recipient` varchar(10) NOT NULL,
`ExtReference` int(20) DEFAULT NULL,
`Date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`Status` varchar(100) NOT NULL,
`userid` int(3) NOT NULL,
`Message` longtext NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=609 ;
With a sample Date set like:
INSERT INTO `History` (`ID`, `Sender`, `Recipient`, `ExtReference`, `Date`, `Status`, `userid`, `Message`) VALUES
(1, '0412345678', '0468888888', 33845909, '2013-03-17 04:17:34', '1', 11, 'Just testing....'),
(2, '0412345678', '0400222333', 33845910, '2013-03-17 04:17:35', '1', 11, 'Amazing'),
(3, '0412345678', '0411111111', 33847419, '2013-03-17 04:46:04', '1', 8, 'Nothing here to see'),
(4, '0412345678', '0400222333', 33850155, '2013-03-17 06:31:57', '1', 12, 'Hello there Mr IT Guru :-)'),
(5, '0400222333', '0412345678', 33850179, '2013-03-17 06:33:21', '1', 12, '[Write message here]'),
(6, '0412345678', '0411111111', 33955423, '2013-03-23 01:26:22', '1', 8, 'Hello Charles'),
(7, '0412345678', '0411111111', 33959071, '2013-03-23 03:08:26', '1', 13, 'Sample Message'),
(8, '0400222333', '0412345678', 33964111, '2013-03-23 05:27:51', '1', 13, 'How do I use this system?'),
(9, '0400222333', '0412345678', 34107503, '2013-03-30 03:13:38', '1', 12, 'Is this thing on?'),
(10, '0412345678', '0401411210', 34230869, '2013-03-05 00:18:09', '1', 16, 'Hello')
(In this example my number is: 0412345678).
SQL Fiddle here: http://sqlfiddle.com/#!2/29197/1/0
I have worked out how to get a list of all the unique numbers used across both the Sender and Recipient columns:
SELECT DISTINCT `Sender` AS phoneID FROM `History`
UNION
SELECT DISTINCT `Recipient` AS phoneID FROM `History`
But I can't work how to attach the latest Date and message to this data.
If I focus on either just messages sent to me or sent by me I can get somewhere with these two:
SELECT `ID`, `Sender`, `Recipient`, MAX(`Date`), `Message` FROM History
GROUP BY Sender
ORDER BY `History`.`Date` DESC
or
SELECT `ID`, `Sender`, `Recipient`, MAX(`Date`), `Message`, `Status` FROM History
GROUP BY Recipient
ORDER BY `History`.`Date` DESC
Any thoughts?
I can recreate the History table layout if needed.
I'll also need to try and join the phone number with a persons name in a Contacts table later on down the track.
Thanks
Charlie
Possibly not the best way but you could combine the two queries you have. Something like:
SELECT `ID`, `Sender`, `Recipient`,`Date`,`Message`,`Status` FROM
(
SELECT `ID`, `Sender`, `Recipient`, `Date`, `Message`,`Status` FROM History
WHERE Sender = "0412345678"
GROUP BY Sender
UNION
SELECT `ID`, `Sender`, `Recipient`, MAX(`Date`), `Message`, `Status` FROM History
WHERE Recipient = "0412345678"
GROUP BY Recipient
) res
GROUP BY res.ID
ORDER BY res.Date DESC
Note that this is for a specific number. You could remove the WHERE clauses if this wasn't needed.
I have two tables:
table category(cat_id,category)
table category_details(cat_id,id,...)
I have sql code like bellow:
$sql=mysql_query("select * from category");
while($rows=mysql_fetch_array($sql)){
$id_count=$rows['cat_id'];
$sql1=mysql_query("select * from category_details where cat_id='$id_count'");
$count=mysql_num_rows($sql1);
}
Can I use like this?
Use JOIN.
Example
SELECT * FROM category LEFT JOIN
category_details ON category_details.cat_id = category.cat_id;
above query will return all the category and associated category detail.
OP Comment Response
SELECT c.name,
IFNULL(sub_c.total, 0) num
FROM category c
LEFT JOIN ( SELECT COUNT(*) total, cat_id
FROM category_details
GROUP BY cat_id
) sub_c ON (sub_c.cat_id = c.cat_id);
Complete Code
<?php
$query = "SELECT c.name,
IFNULL(sub_c.total, 0) num
FROM products_category c
LEFT JOIN ( SELECT COUNT(*) total, cat_id
FROM product
GROUP BY cat_id
) sub_c ON (sub_c.cat_id = c.id)";
$result = mysql_query($query)or die(mysql_error());
echo "<table><tr><td>NameCount</td></tr>";
while($row = mysql_fetch_assoc($result))
{
echo "<tr><td>".$row['name']."(".$row['num'].")"."</td></tr>";
}
echo "</table>";
?>
MySQL Table
ProductCategory Table
CREATE TABLE IF NOT EXISTS `products_category` (
`id` int(11) NOT NULL,
`name` varchar(200) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Dumping data for table `products_category`
--
INSERT INTO `products_category` (`id`, `name`) VALUES
(1, 'Erasmus'),
(2, 'Preston'),
(3, 'Ulric'),
(4, 'Gray'),
(5, 'Joseph'),
(6, 'Merrill'),
(7, 'Alan'),
(8, 'Jeremy'),
(9, 'Solomon'),
(10, 'Andrew'),
(11, 'Galvin'),
(12, 'Craig'),
(13, 'Cameron'),
(14, 'Omar'),
(15, 'Addison');
Product Table
CREATE TABLE IF NOT EXISTS `product` (
`id` int(11) NOT NULL,
`name` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`cat_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Dumping data for table `product`
--
INSERT INTO `product` (`id`, `name`, `cat_id`) VALUES
(1, 'Wesley', 1),
(2, 'Graiden', 2),
(3, 'Cruz', 5),
(4, 'Hayden', 5),
(5, 'Kennedy', 6),
(6, 'Uriah', 8),
(7, 'Alan', 8),
(8, 'Cade', 1),
(9, 'Ryan', 5),
(10, 'Brody', 7);
Above will output
Erasmus(2)
Preston(1)
Ulric(0)
Gray(0)
Joseph(3)
Merrill(1)
Alan(1)
Jeremy(2)
Solomon(0)
Andrew(0)
Galvin(0)
Craig(0)
Cameron(0)
Omar(0)
Addison(0)
Are you looking to use COUNT with GROUP BY:
select c.cat_id, c.cat_name, count(cd.*)
from category c
left join category_details cd
on c.cat_id = cd.cat_id
group by c.cat_id, c.cat_name
This will return each category with the count of category_details associated with it. Using LEFT JOIN will return all categories -- replace with an INNER JOIN if you want only those with details.
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');