Mysql- Select data that has been uploaded more than 2 months - php

I have a table where I upload the users galleries.
I need the users with the gallery.created_at has been uploaded for 2 or more months.
I have tried this but it is giving me 0 results
select `galleries`.`client_id` as `client_id`,
`users`.`first_name` as `first_name` from `galleries`
inner join `users` on `users`.`id` = `galleries`.`client_id`
where `galleries`.`session_id` is null and
`galleries`.`is_video` = '1' and `galleries`.`is_thumb` = '1' and
galleries.created_at >= galleries.created_at - INTERVAL 2 MONTH
`galleries`.`client_id` not in (select `client_id` from
`orders`)

Problem is here:
galleries.created_at >= galleries.created_at - INTERVAL 2 MONTH
and a missing AND.
Perhaps curdate is what you need:
select g.client_id as client_id,
u.first_name as first_name
from galleries g
inner join users u on u.id = g.client_id
where g.session_id is null
and g.is_video = '1'
and g.is_thumb = '1'
and g.created_at <= curdate() - INTERVAL 2 MONTH
and g.client_id not in (
select client_id
from orders
)
I used <= because your requirements states uploaded for 2 or more months.

Related

MySQLi Query 2 tables based on count form one table over 30 days

I am having a trouble with a query.
I need to collect 2 companies from trade_directory where they have status 1 and match 3 categories.
I want to select the 2 companies on the least view first basis counted from company_views within the last 30 days.
SELECT
b.id, b.v_date, b.c_id COUNT(b.c_id) AS v_count
AND b.v_date >= DATE_ADD(NOW(), INTERVAL - 30 DAY)
FROM
company_views b
LEFT JOIN trade_directory v ON v.id = b.c_id
WHERE
v.cat1 = 'cat'
AND v.cat2 = 'rat'
AND v.cat3 = 'hat'
AND v.status = '1'
GROUP BY
b.c_id
ORDER BY
v_count ASC
LIMIT 2
Thanks
Chris
You're using AND operator in SELECT instead of WHERE
AND b.v_date >= DATE_ADD(NOW(), INTERVAL - 30 DAY)
can your try this code
SELECT
b.id, b.v_date, b.c_id COUNT(b.c_id) AS v_count
FROM
company_views b
LEFT JOIN trade_directory v ON v.id = b.c_id
WHERE
v.cat1 = 'cat'
AND v.cat2 = 'rat'
AND v.cat3 = 'hat'
AND v.status = '1'
AND b.v_date >= DATE_ADD(NOW(), INTERVAL - 30 DAY)
GROUP BY
b.c_id
ORDER BY
v_count ASC
LIMIT 2
I changed my approach (using different tables but here it what is working but it is really really slow)
SELECT o.*, COUNT(e.c_id) AS b_count
FROM cat3_entries o
LEFT JOIN company_views e ON e.c_id = o.c_id
AND e.v_date >= DATE_ADD(NOW(), INTERVAL - 30 DAY)
WHERE
o.c3_id = '81'
GROUP BY
o.c_id
ORDER BY
b_count ASC
LIMIT
3;
If I could resolve the speed issue this one would be great.

Select results from table1 based on entries on table2

I have 2 tables;
banner_views (id, b_id, b_date)- this record a banner view every time it gets displayed
banners_dynamic (id, status, static_iname, static_keywords, static_url, static_alt, static_type, static_image, b_views, b_clicks) - stores the banner data
I would like to select 3 banners_dynamic results which have had the least views in the last 7 days.
I did put somethign together (see below) but I realised it was grabbing the total views for all banner rather than uniquely by id.
SELECT *,
(SELECT COUNT(*) FROM banner_views v WHERE v.b_date >= DATE(NOW()) - INTERVAL 7 DAY) as post_count
FROM banners_dynamic b
WHERE static_keywords LIKE '%test%' AND b.status='1' AND b.static_type='1'
ORDER BY post_count ASC LIMIT 3
Can anyone point me in the correct direction?
You must join both banners_dynamic table and your subquery with corresponding banner IDs:
SELECT
b.*, p.b_count
FROM
banners_dynamic b
INNER JOIN (
SELECT
b_id,
COUNT(*) AS b_count
FROM
banner_views v
WHERE
v.b_date >= DATE(NOW() - INTERVAL 7 DAY)
GROUP BY
b_id
) p on p.b_id = b.id
WHERE
b.static_keywords LIKE '%test%'
AND b.`status` = '1'
AND b.static_type = '1'
ORDER BY
p.b_count ASC
LIMIT 3
UPDATE: You can do it even without subquery:
SELECT
b.*, COUNT(v.b_id) AS b_count
FROM
banners_dynamic b
INNER JOIN banner_views v ON v.b_id = b.id
WHERE
v.b_date >= DATE_ADD(NOW(), INTERVAL - 7 DAY)
AND b.static_keywords LIKE '%test%'
AND b.`status` = '1'
AND b.static_type = '1'
GROUP BY
v.b_id
ORDER BY
b_count ASC
LIMIT 3;
If you want to include banners without any views (count=0) then you must do a LEFT JOIN:
SELECT
b.*, COUNT(v.b_id) AS b_count
FROM
banners_dynamic b
LEFT JOIN banner_views v ON v.b_id = b.id
AND v.b_date >= DATE_ADD(NOW(), INTERVAL - 7 DAY)
WHERE
b.static_keywords LIKE '%test%'
AND b.`status` = '1'
AND b.static_type = '1'
GROUP BY
v.b_id
ORDER BY
b_count ASC
LIMIT 3;

Join SQL tables by id field

I have the following sql query on a mysql db which returns all results from wp_client where the form_id is 46 and the date_created is within the last 7 days...
SELECT *
FROM
wp_client WHERE form_id = '46'
and
cast(date_created as date) >= current_date - interval '7' day
I have another table wp_client_detail that stores more information that I would like to be included in the results. The field client_id in wp_client_detail matches up with the field id in wp_client.
I assume I need to use a JOIN command but can't work out how, I have tried...
INNER JOIN
wp_client_detail
ON
wp_client.id=wp_CLIENT_detail.lead_id;
But it's not working, can anyone help?
Nothing wrong with your syntax, just make sure you have it all in the right order:
SELECT *
FROM
wp_client
INNER JOIN wp_client_detail ON
wp_client.id=wp_CLIENT_detail.lead_id
WHERE form_id = '46'
and cast(date_created as date) >= current_date - interval '7' day;
If that syntax doesnt work, then I would suggest that you have problems with your data.
This can also be rendered with an IN
select
*
from
wp_client c
where
form_id = '46'
and cast(date_created as date) >= current_date - interval '7' day
id in (select lead_id from wp_CLIENT_detail)
Or EXISTS:
select
*
from
wp_client c
where
form_id = '46'
and cast(date_created as date) >= current_date - interval '7' day
id exists (select 1 from wp_CLIENT_detail d where c.id = d.lead_id)

GROUP BY this OR that

I've got a web app that uses either Facebook ID or Contact # as an entrant identifier. This is because the client doesn't want those without Facebook to miss out on entering (so there's a web app as well as a Facebook app)
So I'm trying to:
SELECT *, COUNT(*)
FROM `entries_table`
WHERE is_daily_winner = 2
GROUP BY fbid OR contact_no
HAVING COUNT(*) <=4
Basically, what I'm trying to achieve is selecting entries with is_daily_winner = 2 is less than or equal to 4 rows per fbid or contact_no.
Currently I only get one row back, rather than the 3 I was expecting.
What am I doing wrong? or can I even GROUP BY x OR y ?
EDIT: I'm expanding this question, as I forgot to include another clause.
The HAVING COUNT(*) <=4 should be across all entries, but I only need to get those entered within the last day.
I've just tried:
SELECT * FROM `entries_table`
WHERE `timestamp` >= (CURDATE() - INTERVAL 1 DAY) IN
(SELECT * FROM `entries_table`
WHERE is_daily_winner = 2
GROUP BY fbid
HAVING COUNT(*) <=4
UNION
SELECT * FROM `entries_table`
WHERE is_daily_winner = 2
GROUP BY contact_no
HAVING COUNT(*) <= 4)
And I get the error Operand should contain 1 column(s)
This one should do what you want:
SELECT * FROM (
SELECT *, COUNT(*) AS nr FROM `entries_table`
WHERE is_daily_winner = 2
AND `timestamp` >= (CURDATE() - INTERVAL 1 DAY)
GROUP BY fbid
UNION ALL
SELECT *, COUNT(*) AS nr FROM `entries_table`
WHERE is_daily_winner = 2
AND `timestamp` >= (CURDATE() - INTERVAL 1 DAY)
GROUP BY contact_no
) sq
WHERE nr <= 4
I changed UNION to UNION ALL, because UNION implies a DISTINCT. I don't think, that's what you want (or even need, UNION ALL is also faster, because it doesn't have to check if there are duplicate rows).
And note, that selecting * and grouping by something is actually bad practice. By grouping and not having aggregate functions on the columns that are not mentioned in the group by, random rows are picked for each group to display. Just wanted to mention that.
You can Better use UNION like
SELECT *, COUNT(*)
FROM `entries_table`
WHERE is_daily_winner = 2
GROUP BY fbid
HAVING COUNT(*) <=4
UNION
SELECT *, COUNT(*)
FROM `entries_table`
WHERE is_daily_winner = 2
GROUP BY contact_no
HAVING COUNT(*) <=4

INSERT .. SELECT multiple rows from multiple tables

I need to record the number of items requested and issued against each item every day. The purchase_doc table is:
The requested_items table contains item requested as follows:
The movement table contains item requested as follows:
The need output (data to be inserted) is:
One way of doing this is to fetch items issued and requested from the first 2 queries, and then build an array of items issued and requested against each item id, and then insert these values in the daily_movement table, like this:
SELECT n.item_id AS n__item_id, SUM(n.qty) AS qty
FROM requested_items n LEFT JOIN purchase_doc doc ON n.doc_id = doc.id
WHERE (doc.type = 'Item Request' AND doc.created_at > DATE_SUB(NOW(), INTERVAL 24 HOUR))
GROUP BY n.item_id
SELECT n.item_id AS item_id, SUM(n.qty) AS qty
FROM movement n LEFT JOIN purchase_doc doc ON n.doc_id = doc.id
WHERE (doc.type = 'Store Issue' AND doc.created_at > DATE_SUB(NOW(), INTERVAL 24 HOUR))
GROUP BY n.item_id
From these and other SELECTs, I need to insert a single row per item per day containing the qty of requests, issues, etc for this item in this fashion:
INSERT INTO daily_movement date, item_id, requested_qty, issued_qty VALUES ( NOW(), 23, 4, 5), ( NOW(), 25, 5, 5), ( NOW(), 113, 6, 8);
But there will be too many SELECTs (since I also need other activities performed per item), followed by an insert.
My question is: Is it possible to do this via a single SELECT ... INSERT statement. If not, can somebody suggest a more elegant way of doing this
I'm thinking this, but it might be over-simplified:
INSERT INTO `daily_movement`
(`date`, `item_id`, `requested_qty`, `issued_qty`)
SELECT NOW(), `r`.`item_id`, SUM(`r`.`qty`), SUM(`m`.`qty`)
FROM `purchase_doc` `d`
JOIN `requested_items` `r`
ON `r`.`doc_id` = `d`.`id`
LEFT JOIN `movement` `m`
ON `m`.`doc_id` = `d`.`id`
WHERE
(`d`.`type` = 'Item Request' OR `d`.`type` = 'Store Issue')
AND
`d`.`created_at` > DATE_SUB(NOW(), INTERVAL 24 HOUR)
GROUP BY `r`.`item_id`
EDIT
This is my final answer, with a nasty UNION to get around MySQL's lack of FULL OUTER JOIN:
INSERT INTO `daily_movement`
(`date`, `item_id`, `week_no`, `requested_qty`, `issued_qty`)
SELECT *
FROM (
(
SELECT COALESCE(`r`.`item_id`, `a`.`item_id`) AS `item_id`, CURDATE() AS `date`, NULL AS `week_no`, SUM(`r`.`qty`) AS `requests`, COALESCE(`a`.`issued`, 0) AS `issued`
FROM `purchase_doc` `d`
LEFT JOIN `requested_items` `r`
ON `r`.`doc_id` = `d`.`id`
LEFT JOIN (
SELECT `m`.`item_id`, SUM(`m`.`qty`) AS `issued`
FROM `purchase_doc` `d`
JOIN `movement` `m`
ON `m`.`doc_id` = `d`.`id`
WHERE `d`.`type` = 'Store Issue'
AND `d`.`created_at` > DATE_SUB(NOW(), INTERVAL 24 HOUR)
GROUP BY `m`.`item_id`
) `a`
ON `a`.`item_id` = `r`.`item_id`
WHERE `d`.`type` = 'Material Requisition'
AND `d`.`created_at` > DATE_SUB(NOW(), INTERVAL 24 HOUR)
GROUP BY `r`.`item_id`
) UNION DISTINCT (
SELECT COALESCE(`m`.`item_id`, `a`.`item_id`) AS `item_id`, CURDATE() AS `date`, NULL AS `week_no`, COALESCE(`a`.`requests`, 0) AS `requests`, SUM(`m`.`qty`) AS `issued`
FROM `purchase_doc` `d`
LEFT JOIN `movement` `m`
ON `m`.`doc_id` = `d`.`id`
LEFT JOIN (
SELECT `r`.`item_id`, SUM(`r`.`qty`) AS `requests`
FROM `purchase_doc` `d`
JOIN `requested_items` `r`
ON `r`.`doc_id` = `d`.`id`
WHERE `d`.`type` = 'Material Requisition'
AND `d`.`created_at` > DATE_SUB(NOW(), INTERVAL 24 HOUR)
GROUP BY `r`.`item_id`
) `a`
ON `a`.`item_id` = `m`.`item_id`
WHERE `d`.`type` = 'Store Issue'
AND `d`.`created_at` > DATE_SUB(NOW(), INTERVAL 24 HOUR)
GROUP BY `m`.`item_id`
)
ORDER BY `item_id`
) `u`
http://sqlfiddle.com/#!2/3923d/13
you can use the union operator here or here to get all your results in a single select
You can use a query like this -
edited:
INSERT INTO daily_movement(date, item_id, requested_qty, issued_qty)
SELECT i.item_id, SUM(ri.qty) requested_qty, SUM(m.qty) issued_qty FROM
(SELECT item_id FROM requested_items UNION SELECT item_id FROM movement) i
LEFT JOIN (
SELECT n.item_id, n.qty
FROM requested_items n LEFT JOIN purchase_doc doc ON n.doc_id = doc.id
WHERE doc.type = 'Item Request' AND doc.created_at > DATE_SUB(NOW(), INTERVAL 24 HOUR)
) ri
ON ri.item_id = i.item_id
LEFT JOIN (
SELECT n.item_id, n.qty
FROM movement n LEFT JOIN purchase_doc doc ON n.doc_id = doc.id
WHERE doc.type = 'Store Issue' AND doc.created_at > DATE_SUB(NOW(), INTERVAL 24 HOUR)
) m
ON m.item_id = i.item_id
GROUP BY
i.item_id;

Categories