So, i'm trying to query all the data between 2 dates. The date field on my mysql table is at this format: (dd-mm-yyy). So, i'm using php for this.
I made something Like this:
SELECT *
FROM `sells`
WHERE date
BETWEEN '01-01-2013'
AND '04-02-2014'
of course this dates will be variables, but for test on phpmyadmin i didnt achieve the expected result. It gives me all the data ON this dates, let me show.
03-01-2014
03-01-2014
03-01-2014
01-02-2014
01-02-2014
03-02-2014
03-02-2014
03-02-2014
03-02-2014
And of course, all the other datas, but its irrelevant.
I want to bring all the data between a period of dates, how can i fix this error?
Thanks.
Added show table.
Its on my native language, so. (its a domestic system, for my home business
CREATE TABLE `vendas` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_cliente` int(11) NOT NULL,
`cliente` varchar(255) CHARACTER SET latin1 NOT NULL,
`produto` varchar(255) CHARACTER SET latin1 NOT NULL,
`quantidade` int(255) NOT NULL,
`valor` varchar(255) CHARACTER SET latin1 NOT NULL,
`total` varchar(255) CHARACTER SET latin1 NOT NULL,
`metodo` varchar(255) CHARACTER SET latin1 NOT NULL,
`data` varchar(255) CHARACTER SET latin1 NOT NULL,
`status` int(255) NOT NULL DEFAULT '1',
`data_pagamento` varchar(255) CHARACTER SET latin1 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=103 DEFAULT CHARSET=latin1 COLLATE=latin1_spanish_ci
This is what happens when you store your dates as strings instead of as dates. You get query hell.
You will need to convert your string dates into the proper format and then change your search criteria to also be in the proper date format (YYYY-MM-DD):
WHERE STR_TO_DATE(date, "%d-%m-%Y")
BETWEEN '2013-01-01'
AND '2014-02-04'
Try this -
SELECT *
FROM `sells`
WHERE DATE_FORMAT(<your column name>,'%m-%d-%Y') >= '01-01-2013'
and DATE_FORMAT(<your column name>,'%m-%d-%Y') <= '04-02-2014';
And this will also work --
SELECT *
FROM `sells`
WHERE <your column name> between DATE_FORMAT('01-01-2013','%m-%d-%Y')
and DATE_FORMAT('04-02-2014','%m-%d-%Y');
Related
I have the following table in my database.
CREATE TABLE `market_messages` (
`pm_id` int(11) NOT NULL,
`pm_item_index` int(11) DEFAULT NULL,
`pm_item_id` varchar(11) COLLATE utf8_polish_ci NOT NULL,
`pm_item_sender` varchar(30) COLLATE utf8_polish_ci DEFAULT NULL,
`pm_item_client` varchar(30) COLLATE utf8_polish_ci NOT NULL,
`pm_item_sender_id` varchar(30) COLLATE utf8_polish_ci DEFAULT NULL,
`pm_item_client_id` varchar(30) COLLATE utf8_polish_ci DEFAULT NULL,
`pm_item_name` varchar(80) COLLATE utf8_polish_ci DEFAULT NULL,
`pm_sender` varchar(30) COLLATE utf8_polish_ci DEFAULT NULL,
`pm_receiver` varchar(30) COLLATE utf8_polish_ci DEFAULT NULL,
`pm_entry_start` timestamp NULL DEFAULT NULL,
`pm_message` text COLLATE utf8_polish_ci DEFAULT NULL,
`pm_index` int(11) DEFAULT NULL,
`pm_type` varchar(10) COLLATE utf8_polish_ci DEFAULT NULL,
`pm_user_offer` varchar(50) COLLATE utf8_polish_ci DEFAULT NULL,
`pm_status` varchar(30) COLLATE utf8_polish_ci DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci;
Now I'm trying to group messages and order in specific sequence on the list.
I need to list comments based on last entry into m_entry_start column which type is timestamp.
Here is my query, but result is not good:
$sql = "SELECT *
FROM market_messages
WHERE (pm_item_client = '$user_name'
AND pm_item_client_id = '$user_id')
OR (pm_item_sender = '$user_name'
AND pm_item_sender_id = '$user_id')
GROUP BY pm_item_id, pm_item_client_id, pm_item_sender_id
ORDER BY MAX(pm_entry_start) DESC;";
Result is not this what I have expected.
Echo $fetch['m_entry_start']; doesn't give me right result.
I'm expecting max value of pm_entry_start timestamp from each group, but in results is taken probably first value. Additionally `pm_entry_start' is echoed with ago() function to show elapsed time. Ago function is converting timestamp in format 2021-10-01 10:00:19 to something like 3 minutes ago, or 1 day ago etc.
This is example result:
$user_name
ago(timestamp)
Adam
1 month ago
Eve
23 days ago
Terese
2 months ago
Mark
5 days ago
Monica
3 min ago
I expecting results in order based on timestamp in descending order so from the last entry to the first.
$user_name
ago(timestamp)
Monica
3 min ago
Mark
5 days ago
Eve
23 days ago
Adam
1 month ago
Terese
2 months ago
How to write the good query? I have started programming in PHP last year. This project is my first with use of MySQL database. Any help appreciated.
Finally I have found the solution:
SELECT ..., MAX(pm_entry_start) AS pm_entry_start FROM ... was missing part of MySQL query.
Before I have tried only:
SELECT ..., MAX(pm_entry_start) FROM ...
This question already has answers here:
Access value in result set row where the value comes from a MySQL function call
(2 answers)
Closed 1 year ago.
I have a query selecting table including date field, and I want to convert the date into the format dd/MM/YYYY. I retrieved data like this...
$listNews = "
SELECT p.images
, p.title
, p.category
, DATE_FORMAT(p.create_date, '%d/%M/%Y')
FROM posts p
WHERE p.status = 'Active'
AND p.category = 'Tin Tức'
ORDER
BY id DESC
";
$stmtNews = $sqlCon->prepare($listNews);
$stmtNews->execute();
$newsPanel = $stmtNews->fetchAll();
$stmtNews->closeCursor();
but the result was showing that the variable create_date could not defined, when I take out the DATE_FORMAT It works fine. I do not know where I was wrong. Can you guys help me out that problem.
this is my table structure
DROP TABLE IF EXISTS `posts`;
CREATE TABLE `posts` (
`id` int(50) NOT NULL AUTO_INCREMENT,
`link` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`title` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`content` longtext CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`author` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`create_date` date NOT NULL,
`status` enum('Active','Unactive') CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`category` enum('tin-tuc','su-kien','hoat-dong','huong-dan','ho-tro') CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`images` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`, `images`) USING BTREE,
INDEX `image_code`(`images`) USING BTREE,
CONSTRAINT `image_code` FOREIGN KEY (`images`) REFERENCES `uploads` (`code`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Compact;
Using %m if you want to output a month as a numeric value.
DATE_FORMAT(posts.create_date, '%d/%m/%Y')
So i have a table that has multiple date time columns and i am trying to select certain records based on a certain date using
SELECT * FROM `posdata` WHERE `CommissionDate` >= '2019-01-01 00:00:00'
the table structure
CREATE TABLE `posdata` (
`ID` int(11) NOT NULL,
`DISTYNAME` varchar(30) DEFAULT NULL,
`ENDCUST` varchar(75) DEFAULT NULL,
`MFGCUST` varchar(50) DEFAULT NULL,
`EXTPRICE` double DEFAULT NULL,
`POSPERIOD` datetime DEFAULT NULL,
`PAYMENTDATE` date DEFAULT NULL,
`QTY` double DEFAULT NULL,
`UNITCOST` double DEFAULT NULL,
`UNITPRICE` double DEFAULT NULL,
`COMMISSION` double DEFAULT NULL,
`SALESORDER` varchar(40) DEFAULT NULL,
`PO` varchar(40) DEFAULT NULL,
`POLineItem` varchar(20) DEFAULT NULL,
`ENTRYDATE` datetime DEFAULT NULL,
`AdjustedCommission` int(11) DEFAULT NULL,
`CustomerPart-NO` varchar(50) DEFAULT NULL,
`CommissionDate` datetime DEFAULT NULL,
`EXTCOST` double DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `posdata`
ADD PRIMARY KEY (`ID`),
ADD KEY `CommissionDate` (`CommissionDate`);
ALTER TABLE `posdata` ADD FULLTEXT KEY `posdata_endcustomer_index` (`ENDCUST`);
a very weird thing happens, it returns all the fields as required, but the CommissionDate column has only '2019-01-01 00:00:00' as the date. The actual CommissionDate column in the database has only '2016-01-01 00:00:00' as the data.
I am using phpmyadmin to to run this query and have used the search filter on that and it always gives me the same result whether i run it thorough a php script or phpmyadmin. What am i doing wrong ?
In your query SELECT * FROM 'posdata' means get all the fields from the table post data and then the next WHERE clause applies.If you only want to get the data from CommissionDate column
Change Your Query to
SELECT `CommissionDate` FROM `posdata`
In the way you get the desired data.
the query was correct can some one delete this question !
I have a couple of tables in MySQL that I'm working with that do not have any relation. They do have a column of similar data (Postal Codes / Zip Codes).
What I have to do with these tables is compare the postal codes from one table compare them to the postal codes from the first table and count them.
For Example.
Table A has a postal code of T0A and T0B (I use only the first three characters in the postal code as this is all I need to compare against)
Table B has 13 rows where the postal code matches T0A and 3 rows where the postal code matches T0B.
So the outcome would look like:
T0A = 13
T0B = 3
HOWEVER, then I need to take these and separate them by city, so since both T0A and T0B could be one city I would need to take those and add them together and get something like.
Edmonton = 16
I've been doing this with for loops and arrays. So I'm reading the data from table A into one array and the data from table b into another array. Then I compare the postal codes from table B to the postal codes in table A using nested for loops in order to count the number of occurrences of the postal codes and then I store them in another array. This is all fine and dandy however now I'm a bit stuck trying to separate the counts into their correct cities and I'm sitting here thinking there must be an easier way to do this. Does anyone have any suggestions, am I going about this all wrong?
Structure - Table A
jos_postalzip_redirect | CREATE TABLE `jos_postalzip_redirect` (
`id` int(11) NOT NULL auto_increment,
`country_code` varchar(2) NOT NULL,
`prov_state_code` varchar(2) NOT NULL,
`city` varchar(60) NOT NULL,
`postal_zip` varchar(6) NOT NULL,
`email_address` varchar(60) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=739 DEFAULT CHARSET=utf8 |
Structure - Table B
jos_form_submitteddata_form1 | CREATE TABLE `jos_form_submitteddata_form1` (
`id` int(11) NOT NULL auto_increment,
`bf_status` varchar(20) collate utf8_bin NOT NULL,
`bf_user_id` int(11) NOT NULL,
`FIELD_1` varchar(255) collate utf8_bin NOT NULL,
`FIELD_2` varchar(255) collate utf8_bin NOT NULL,
`FIELD_3` varchar(255) collate utf8_bin NOT NULL,
`FIELD_4` varchar(255) collate utf8_bin NOT NULL,
`FIELD_5` varchar(255) collate utf8_bin NOT NULL,
`FIELD_6` varchar(255) collate utf8_bin NOT NULL,
`FIELD_7` varchar(255) collate utf8_bin NOT NULL,
`FIELD_8` varchar(255) collate utf8_bin NOT NULL,
`FIELD_23` varchar(255) collate utf8_bin NOT NULL,
`FIELD_24` varchar(255) collate utf8_bin NOT NULL, //THIS IS THE POSTAL CODE FIELD
`FIELD_28` varchar(255) collate utf8_bin NOT NULL,
`FIELD_29` varchar(255) collate utf8_bin NOT NULL,
`FIELD_30` varchar(255) collate utf8_bin NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4044 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
Just an abstraction of what I understood. You may need to adjust it according to your needs.
In this example I will assume that FIELD_1 in table B is a postal code.
Count by postal code:
select
left(ta.postal_zip, 3) p_code, count(*)
from
jos_form_submitteddata_form1 tb
join jos_postalzip_redirect ta on left(tb.field_1, 3) = left(ta.postal_zip, 3)
group by
p_code
Count by city:
select
ta.city, count(*)
from
jos_form_submitteddata_form1 tb
join jos_postalzip_redirect ta on left(tb.field_1, 3) = left(ta.postal_zip, 3)
group by
ta.city
I have a MySQL table, with the field ('ad_id').
I store variables in different fields with PHP.
One of these fields, the 'ad_id' field as mentioned above, stores the same exact nr over and over again, no matter what the "REAL" name is in the PHP file.
Example: $ad_id= 12345;
When trying to store this, the number 11111 is stored. ALWAYS, no matter what I change the $ad_id variable to.
I even echo the $ad_id variable, and it actually IS 12345, but MySQL stores a value of 11111 anyway.
CODE:
mysql_query("INSERT INTO cars_db
(ad_id, area, area_community, price, year, mileage, gearbox, fuel, poster_name, poster_email, poster_tel, poster_password, private_or_company, headline, description, salebuy, total_pics, changeable, hide_tel)
VALUES ('$ad_id', '$area', '$kommun', '$price', '$year', '$mile', '$gearbox', '$fuel', '$name', '$email', '$tel', '$ad_passw', '$priv_or_comp', '$headline', '$ad_text', '$forsale', '$nr_of_pics', '$changeable', '$hide_tel')");
Update with MySQL table info:
CREATE TABLE `cars_db` (
`id` int(7) NOT NULL AUTO_INCREMENT,
`ad_id` int(13) NOT NULL,
`area` varchar(40) COLLATE utf8_swedish_ci NOT NULL,
`area_community` varchar(50) COLLATE utf8_swedish_ci NOT NULL,
`price` int(9) NOT NULL,
`year` int(4) NOT NULL,
`mileage` int(6) NOT NULL,
`gearbox` varchar(12) COLLATE utf8_swedish_ci NOT NULL,
`fuel` varchar(12) COLLATE utf8_swedish_ci NOT NULL,
`insert_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`poster_name` varchar(40) COLLATE utf8_swedish_ci NOT NULL,
`poster_email` varchar(50) COLLATE utf8_swedish_ci NOT NULL,
`poster_tel` varchar(20) COLLATE utf8_swedish_ci NOT NULL,
`poster_password` varchar(15) COLLATE utf8_swedish_ci NOT NULL,
`private_or_company` int(2) NOT NULL,
`headline` varchar(60) COLLATE utf8_swedish_ci NOT NULL,
`description` text COLLATE utf8_swedish_ci NOT NULL,
`salebuy` int(2) NOT NULL,
`total_pics` int(2) NOT NULL,
`changeable` int(1) NOT NULL,
`hide_tel` int(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=134 DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci
Update again:
I tried setting the $ad_id=012345678901 (12 digits) and it didn't work. Once again same problem. But then I tried setting $ad_id=555555 and it worked, MySQL stored that information...
The length is the problem I think.
Is there a maximum or something?
Is there hidden decimals I don't know about. If so, how can I round it?
Update string insert into:
All looks fine. Even the value of the $ad_id is ok, 12 digits, but that's not what's inserted into MySQL, because that's a different nr and it is only 10 digits!
- AC
- STEREO
- VINTERDÄCK', 'Säljes', '0', '0', '0')
Camran you number is too big
if you take a look at mysql documentation here it says:
Type N byte min value max value
INT 4 -2147483648 2147483647
You should generate a smaller random number also if you need that to be random you can use the MySQL RAND function.
The number 668244234626 is out of range for an integer. It's being truncated to the max value for an integer, which is 2147483647.
If you run your INSERT statement in a MySQL client, you get this result:
Query OK, 1 row affected, 1 warning (0.00 sec)
Then if you show warnings:
mysql> show warnings;
+---------+------+------------------------------------------------+
| Level | Code | Message |
+---------+------+------------------------------------------------+
| Warning | 1264 | Out of range value for column 'ad_id' at row 1 |
+---------+------+------------------------------------------------+
I notice you declare ad_id INT(13). Note that the argument 13 has no effect on the range of values that you can store in the integer. A MySQL integer data type always stores a 32-bit number, and the value you gave is greater than 232.
If you need to store larger values, you should use BIGINT.