Retrieve last 3 years of data with custom date - php

Here's my query:
mysql> select * from jobs where datediff(now(),str_to_date(last_modified,'%M %d,%Y'))>=1095;
I get 0 results
mysql> select max(last_modified) from jobs;
+--------------------+
| max(last_modified) |
+--------------------+
| 9/9/2013 |
+--------------------+
1 row in set (0.06 sec)
mysql>
It doesn't seem to be working to well and Im not sure why. I think it has to do with the original formatting of the last_modified column
UPDATE
mysql> select distinct(last_modified) from jobs where datediff(now(),str_to_date(last_modified,'%m/%d/%Y'))>=1095 limit 10;
+---------------+
| last_modified |
+---------------+
| 12/4/2003 |
| 12/5/2003 |
| 12/6/2003 |
| 12/8/2003 |
| 12/9/2003 |
| 12/10/2003 |
| 12/11/2003 |
| 12/12/2003 |
| 12/13/2003 |
| 12/14/2003 |
+---------------+
10 rows in set (0.00 sec)
mysql>

Use str_to_date(last_modified,'%m/%d/%Y') instead of str_to_date(last_modified,'%M %d,%Y')

Related

SQL query column with cookie data

My cookie member names are the first and last name of the user with a space in between. I'm trying to get the user ID from the row with the following query:
SELECT * FROM dbo.users WHERE firstname IN('{$_COOKIE['member_name']}') AND lastname IN('{$_COOKIE['member_name']}')
I'm not getting any results from this, and I can't figure out what I'm doing wrong. The LIKE operator isn't working either.
why using IN() ?
i have try like this query, can work for my data
maybe you can check table column data type and output query SQL.
(root#localhost) [test]> select * from a;
+------+------+-------+
| team | type | value |
+------+------+-------+
| A | 0 | 10 |
| A | 1 | 5 |
| B | 0 | 10 |
| B | 0 | 10 |
| A | 1 | 20 |
| B | 1 | 20 |
+------+------+-------+
6 rows in set (0.00 sec)
(root#localhost) [test]> select * from a where team in ('A') and type in ('0');
+------+------+-------+
| team | type | value |
+------+------+-------+
| A | 0 | 10 |
+------+------+-------+
1 row in set (0.00 sec)

Join table fields with different ID in PHP

How to join many fields with different ID's in only one?
I have this MySQL table:
--------------------------------
| *UDH* | *Text* |
--------------------------------
| 050003B90301 | Hi my name is A|
--------------------------------
| 050003B90302 | rmin and I wan |
--------------------------------
| 050003B90303 | t be your frien |
--------------------------------
The UDH field is different but I need join the text field to copy to other table, the result must to be like this:
______________________________________________________________
| UDH | Text |
--------------------------------------------------------------
| 1 | Hi my name is Armin and I want be your frien |
---------------------------------------------------------------
Do you know a PHP sentence or other method to make something like this?
Use GROUP_CONCAT(). Like this:
select '1' as UDH, group_concat(`Text` separator '') as `Text` from myTable;
If you need to order by the UDH column, you can do that within the group_concat() call like this:
select '1' as UDH, group_concat(`Text` order by UDH separator '') as `Text` from myTable;
I just validated this query with the following test code based on your example:
mysql> create table myTable (UDH varchar(32), `Text` text) engine=innodb;
Query OK, 0 rows affected (0.05 sec)
mysql> insert into myTable (UDH, `Text`) values ('050003B90301', 'Hi my name is A'), ('050003B90302', 'rmin and I wan'), ('050003B90303', 't be your frien');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from myTable;
+--------------+-----------------+
| UDH | Text |
+--------------+-----------------+
| 050003B90301 | Hi my name is A |
| 050003B90302 | rmin and I wan |
| 050003B90303 | t be your frien |
+--------------+-----------------+
3 rows in set (0.00 sec)
mysql> select '1' as UDH, group_concat(`Text` order by UDH separator '') as `Text` from myTable;
+-----+----------------------------------------------+
| UDH | Text |
+-----+----------------------------------------------+
| 1 | Hi my name is Armin and I want be your frien |
+-----+----------------------------------------------+
1 row in set (0.01 sec)

MYSQL RANDOM SELECT UNIQUE ROWS - Excluding previously selected rows

I have a table of 16K entries
I want to extract random 44 entries
but I don't want to repeat the same entries more then once (ever)
so i have a per-user list that keeps the already used 'IDs' as a comma-separated string in a table.
and I use that list to SELECT ... NOT IN (used_IDs)
The issue is that this list is getting too big and the sql call fails because of size i believe
Any idea on how to do that more usefully?
Questions table:
+------+-------+-------+
| id | Qtext | Tags |
+------+-------+-------+
Test table:
+------+-------+
| id | QIDs |
+------+-------+
Results table:
+------+-------+-------+
| id | tID | uID |
+------+-------+-------+
I need to select unique random values from Questions table based on the results table. (which associates test ID with Question IDs)
Currently trying to use:
SELECT DISTINCT `questions`.`ID`
FROM `questions`, `tests`, `results`
WHERE
`questions`.`ID` NOT IN (`tests`.`qIDs`)
AND `results`.`uID` = 1 AND `tests`.`ID` = `results`.`tID`
AND 4 IN ( `questions`.`tags`)
AND "http://www.usmlestep2qna.com" = `provider`
ORDER BY RAND() LIMIT 27;
Any ideas?
Instead of placing the used user Id values in a comma-separated string in one column, you could create a tall table to store them. This should yield better preformance
Rather than using a single row with a (potentially huge) CSV, why not use a nicely indexed table and an outer join to pick unmatched records. I have an example from my test database:
mysql> select * from first;
+------+-------+
| id | title |
+------+-------+
| 1 | aaaa |
| 2 | bbbb |
| 3 | cccc |
| 4 | NULL |
| 6 | gggg |
+------+-------+
5 rows in set (0.00 sec)
mysql> select * from second;
+------+----------+------+------+-------+------+
| id | first_id | one | two | three | four |
+------+----------+------+------+-------+------+
| 1 | 1 | 3 | 0 | 4 | 6 |
| 1 | 2 | 4 | 4 | 1 | 2 |
| 3 | 3 | 1 | NULL | 3 | 4 |
+------+----------+------+------+-------+------+
3 rows in set (0.00 sec)
mysql> select a.id from first a join second b on a.id=b.first_id;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)
mysql> select a.id from first a
left outer join second b on a.id=b.first_id where b.first_id is null;
+------+
| id |
+------+
| 4 |
| 6 |
+------+
2 rows in set (0.00 sec)
This should improve your performance rather nicely.

How to check query_cache_type value on my server

I have checked the status of query cache by using this query SHOW VARIABLES LIKE ‘%query_cache%’; . Likewise is there any query or command for checking the value of query_cache_type. Any help will be highly appreciated.
Try any of these:
SHOW GLOBAL VARIABLES LIKE 'query_cache_type';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| query_cache_type | ON |
+------------------+-------+
SELECT * FROM INFORMATION_SCHEMA.GLOBAL_VARIABLES WHERE VARIABLE_NAME='query_cache_type';
+------------------+----------------+
| VARIABLE_NAME | VARIABLE_VALUE |
+------------------+----------------+
| QUERY_CACHE_TYPE | ON |
+------------------+----------------+
SELECT ##query_cache_type;
+--------------------+
| ##query_Cache_type |
+--------------------+
| ON |
+--------------------+
SELECT ##query_cache_type = 'ON' AS is_it_on;
+----------+
| is_it_on |
+----------+
| 1 |
+----------+
Try
SHOW VARIABLES LIKE 'query_cache_type';

MySql PHP results

Hi i'm working on some code with mysql and need to display the mysql results with php..
MySQL
select distinct(year(Cataloged_Date)) from records;
+------------------------+
| (year(Cataloged_Date)) |
+------------------------+
| 2009 |
+------------------------+
1 row in set (0.00 sec)
PHP
foreach($query->result() as $show){
$data[$i] = $show->Cataloged_Date;
$i++;
}
I'm using codeigniter for the php. Using $show->Cataloged_Date will not return anyting. I'm thinking its $show-> something to display the results...Just cant get it right now...
You need to provide a explicit name or alias for your field in the mysql query - when you apply functions to a column then it's non-obvious what the column name will be.
Try this:
//on MySQL
select distinct(year(Cataloged_Date)) as "Cat_Date" from records;
<?php
foreach($query->result() as $show){
$data[$i] = $show->Cat_Date;
$i++;
}
?>
You can apply an alias to any "value" on your select, be it a column name or the result of a function.
Just do
SELECT something AS YourAlias ...
To give you a clear example:
mysql> select 1;
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.00 sec)
#A simple value can be given an alias
mysql> select 1 as "Number";
+--------+
| Number |
+--------+
| 1 |
+--------+
1 row in set (0.00 sec)
mysql> select max(val) from my_values;
+----------+
| max(val) |
+----------+
| 4 |
+----------+
1 row in set (0.00 sec)
#A function
mysql> select max(val) as "max_val" from my_values;
+---------+
| max_val |
+---------+
| 4 |
+---------+
1 row in set (0.00 sec)
#or even a plain column
mysql> select val as "lav" from my_values;
+------+
| lav |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
+------+
4 rows in set (0.00 sec)
Yes this is normally true but i'm using Codeigniter php framework. I did get it working with
$query->first_row() as $show
Then just echo $show and the results will display without you needing to know the rows name..

Categories