Generating a list of years/months in MySQL - mysql

Currently for my blog I get a list of years/dates using the following code:
$monthList = array(
1 => 'January',
2 => 'February',
3 => 'March',
4 => 'April',
5 => 'May',
6 => 'June',
7 => 'July',
8 => 'August',
9 => 'September',
10 => 'November',
11 => 'October',
12 => 'December'
);
$nav = array();
for ($year = 2009; $year <= date('Y'); $year++) {
foreach ($monthList as $id => $month) {
$this->tru->query->runRaw(array(
'name' => 'get-headline-count',
'sql' => 'SELECT
COUNT(id) as count
FROM
wp_'.$this->getId().'_posts
WHERE
post_status = \'publish\' AND
post_type = \'post\' AND
post_parent = \'0\' AND
YEAR(post_date) = '.$year.' AND
MONTH(post_date) = '.$id.'',
'connection' => 'article'
));
$temp = $this->tru->query->getArray('get-headline-count');
if ($temp['count'] > 0) {
$nav[$year][$id] = $temp['count'];
}
}
}
krsort($nav);
return array(
'month' => $monthList,
'nav' => $nav
);
This was all great at first, but now that I've got a few years worth of data and a few months this isn't going to cut it anymore. I've never done anything like this before, I usually deal with timestamps.

You can replace the loop with this one query
SELECT
YEAR(post_date) as yr, MONTH(post_date) as mn, COUNT(id) as count
FROM
wp_posts
WHERE
post_status = 'publish' AND
post_type = 'post' AND
post_parent = '0'
GROUP BY YEAR(post_date), MONTH(post_date)

Related

How to get max value of multiple column in a single row?

trying to get max value of months column in a single row
for example i want to get max value from jan~ dec from first column which is currently 5 is the max value
preferably do it in laravel
See the PHP max function
To get the month name(s) (remember more than one month could have the maximum value), you'll need to implement a loop.
$max_day = max(
$record->jan,
$record->feb,
$record->mar,
$record->apr,
$record->may,
$record->jun,
$record->jul,
$record->aug,
$record->sep,
$record->oct,
$record->nov,
$record->dec
);
$columns = [
'jan' => 'January',
'feb' => 'February',
'mar' => 'March',
'apr' => 'April',
'may' => 'May',
'jun' => 'June',
'jul' => 'July',
'aug' => 'August',
'sep' => 'September',
'oct' => 'October',
'nov' => 'November',
'dec' => 'December',
];
$max_months = [];
foreach ($columns as $column_name => $month_name) {
if ($record->{$column_name} === $max_day) {
$max_months[] = $month_name;
}
}
// $max_months e.g. ['January', 'February']

MySQL table order by values from relation table by newest date

Is it possible to select from mySQL table rows ordered by joined table values by current date?
Maybe I show it. I Have two tables:
One country have many scores GROUPED BY date. Is it possible to sort countries by current date scores?
I have countires with scores from table in PHP arrays:
$countries = array(
'England' =>
array('name', 'tag',
'scores' => array(
0 => array('date' => '2018-07', 'score' => 100),
1 => array('date' => '2018-08', 'score' => 50)
)),
'USA' =>
array('name', 'tag',
'scores' => array(
0 => array('date' => '2018-08', 'score' => 50),
1 => array('date' => '2018-09', 'score' => 20)
)
),
'France' =>
array('name', 'tag',
'scores' => array(
0 => array('date' => '2018-08', 'score' => 30),
1 => array('date' => '2018-09', 'score' => 100)
)
)
);
I need to select countries ordered by scores for current date. Lets say current date is 2018-09. I need to search countries and sort them like this:
$countries = array(
'France' =>
array('name', 'tag',
'scores' => array(
0 => array('date' => '2018-08', 'score' => 30),
1 => array('date' => '2018-09', 'score' => 100)
)
),
'USA' =>
array('name', 'tag',
'scores' => array(
0 => array('date' => '2018-08', 'score' => 50),
1 => array('date' => '2018-09', 'score' => 20)
)
),
'England' =>
array('name', 'tag',
'scores' => array(
0 => array('date' => '2018-07', 'score' => 100),
1 => array('date' => '2018-08', 'score' => 50)
)),
);
England is last becouse havent scores for date '2018-09'
Second is France becouse for date '2018-09' have 100 points
USA is last becosue for date '2018-09' have 20 points
SELECT c.name, s.date, s.score FROM country c LEFT JOIN score s ON s.country_id=c.id GROUP BY date ORDER BY (??)
We could do something like this, to get the latest score date for each country_id, along with the score for that latest date, and then order by those values:
SELECT c.name
, s.date
, s.score
FROM country c
LEFT
JOIN score s
ON s.country_id = c.id
LEFT
JOIN ( SELECT m.country_id
, MAX(m.date) AS max_date
FROM score m
GROUP
BY m.country_id
) n
ON n.country_id = c.id
LEFT
JOIN score o
ON o.country_id = n.country_id
AND o.date = n.max_date
ORDER
BY o.date DESC
, o.score DESC
, c.name ASC
, s.date ASC
Note that this assumes that the (country_id,date) tuple is unique in the score table. If we don't have that guarantee, we can tweak the query to accommodate that. Something like this:
SELECT c.name
, s.date
, s.score
FROM country c
LEFT
JOIN score s
ON s.country_id = c.id
LEFT
JOIN ( SELECT o.country_id
, o.date
, MAX(o.score) AS max_date_score
FROM ( SELECT m.country_id
, MAX(m.date) AS max_date
FROM score m
GROUP
BY m.country_id
) n
JOIN score o
ON o.country_id = n.country_id
AND o.date = n.max_date
GROUP
BY o.country_id
, o.date
) p
ON p.country_id = c.id
ORDER
BY p.date DESC
, p.max_date_score DESC
, c.name ASC
, s.date ASC
, s.score DESC

how to combine 2 sql queries to get 1 result

SELECT users.name, users.registrationdate, users.city, users.status
FROM users, referrals
WHERE users.username= referrals.referrals AND referrals.username='user_name'
SELECT adearning, reffearning
FROM earnings WHERE usersname='user_name33'
How can I combine these two queries as if first query condition doesn't match and doesn't run.. then the second query should run and return some value if condition match
Try this
$query1 = $this->db->query("SELECT users.name, users.registrationdate, users.city, users.status
FROM users, referrals
WHERE users.username = referrals.referrals AND referrals.username='user_name'")
$result1 = $query1->result_array();
$query2 = $this->db->query("SELECT adearning, reffearning FROM earnings WHERE usersname='user_name33' ")
$result2 = $query2->result_array();
$dataArray = array_merge($result1, $result2);
if want to check the result, end of the line add this
print_r($dataArray);die;
On this it will print all of them in one array
Example
$result1 = array(
'0' => 'sjhccsd',
'1' => 'ddd',
'2' => 'df',
'3' => 'dfd',
);
$result2 = array(
'0' => 'sjhccsd',
'1' => 'ddd',
'2' => 'df',
'3' => 'dfd',
);
$dataArray = array_merge($result1, $result2);
print_r($dataArray);
Output : Array ( [0] => sjhccsd [1] => ddd [2] => df [3] => dfd [4] => sjhccsd [5] => ddd [6] => df [7] => dfd )

cakephp join from sorted and limited row

I have a working SQL query i want to figure out what would be the CakePHP query builder equivalent to work the exact same way?
SELECT customers.*, customer_reps.created FROM customers
LEFT JOIN customer_reps ON customer_reps.id =
(
SELECT id FROM customer_reps WHERE customer_id = customers.id
ORDER BY id ASC LIMIT 1
)
WHERE created >= 1475298000
AND created <= 1476217836
AND agents_id = 4
So essentially i am selecting all columns from "customers" and then i want only the "created" timestamp field of the FIRST "customer_reps" table for matching customer.
Documentation on CakePHP doesn't seem to explain how to do the select within select for sorting as this. I tried using the "hasMany" relational stuff but i was not able to find how to just get the "first" customer_reps entry to add to the main query for use in WHERE clause.
Thanks
try this code as a starting point you just need to group it by CustomerRep.customer_id because the order is already ascending if you're customer_reps table id is auto incremented already
$this->Customer->find('all', array(
'fields' => arrray(
'Customer.*',
'CustomerRep.created'
),
'joins' => array(
array(
'type' => 'LEFT',
'table' => 'customer_reps',
'alias' => 'CustomerRep',
'conditions' => 'Customer.id = CustomerRep.customer_id'
)
)
'conditions' => array(
'Customer.created >=' => '1475298000',
'Customer.created <=' => '1476217836',
'Customer.agentds_id' => 4
),
'group' => 'CustomerRep.customer_id'
));
Are you familiar with cakephp query builder? then if yes you should understand this flow
Step 1: SELECT customers.*, customer_reps.created FROM customers equivalent in cakephp is
$this->Customer->find('all', array(
'fields' => array(
'Customer.*',
'CustomerReply.created'
)
);
Note: CustomerReply.created is from join table of customer_reps which I just alias CustomerReply
Step 2: LEFT JOIN customer_reps ON customer_reps.id =
(
SELECT id FROM customer_reps WHERE customer_id = customers.id
ORDER BY id ASC LIMIT 1
) equivalent in cakephp is
'joins' => array(
array(
'table' => 'customer_reps',
'type' => 'left',
'alias' => 'CustomerReply',
'conditions' => array(
'CustomerReply.id = (SELECT id FROM customer_reps WHERE customer_id = Customer.id ORDER BY id ASC LIMIT 1)'
)
))
Step 3: WHERE created >= 1475298000
AND created <= 1476217836
AND agents_id = 4 equivalent in cakephp is
'conditions' => array(
'Customer.created >=' => 1475298000,
'Customer.created <=' => 1476217836,
'Customer.agents_id' => 4
)
);
so your cakephp query builder would be like this
$query = $this->Customer->find('all', array(
'fields' => array(
'Customer.*',
'CustomerReply.created'
),
'joins' => array(
array(
'table' => 'customer_reps',
'type' => 'left',
'alias' => 'CustomerReply',
'conditions' => array(
'CustomerReply.id = (SELECT id FROM customer_reps WHERE customer_id = Customer.id ORDER BY id ASC LIMIT 1)'
)
)),
'conditions' => array(
'Customer.created >=' => 1475298000,
'Customer.created <=' => 1476217836,
'Customer.agents_id' => 4
)
));

Wordpress meta_query not working

I want to all records from my postmeta table where meta_key = 'teljesitmeny' and meta_value between 0 and 150.
I make this query, but it's not working:
$argsg = array(
'post_type' => 'termek',
'meta_query' => array(
array(
'key' => 'teljesitmeny',
'value' => array(0, 150),
'compare' => 'BETWEEN'
),
),
);
$scheduled = '';
$scheduled = new WP_Query( $argsg );
It's only post one query, where meta_value is 150.
Datas in postmeta table:
Datas after execute the query:
What did i wrong?
Update:
I found the solution:
SELECT *
FROM `eopwrqjnc_postmeta`
WHERE `meta_key` LIKE 'teljesitmeny'
AND `meta_value`
BETWEEN CAST( 0 AS UNSIGNED )
AND CAST( 150 AS UNSIGNED )
LIMIT 0 , 30