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)
Related
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.
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.
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;
I want to get data added in the last 24 hours I already tried with this query but it shows nothing:
$result = $db->query("
SELECT reclamations.* , customers.*
FROM reclamations
LEFT JOIN customers ON reclamations.id_customer = customers.id
ORDER BY reclamations.code
WHERE reclamation_date = DATE_SUB( NOW() - INTERVAL 24 HOUR"));
You currently have a PHP and MySQL syntax error because you having a closing parenthesis the wrong side of your closing double-quote.
How about the following, where the column is more than or equal to now minus 1 day:
$result = $db->query("
SELECT reclamations.* , customers.*
FROM reclamations
LEFT JOIN customers
ON reclamations.id_customer = customers.id
ORDER BY reclamations.code
WHERE reclamation_date >= NOW() - INTERVAL 1 DAY;
");
Try this:
$result = $db->query("
SELECT reclamations.* , customers.*
FROM reclamations
LEFT JOIN customers ON reclamations.id_customer = customers.id
ORDER BY reclamations.code
WHERE reclamation_date BETWEEN DATE_SUB(NOW(), INTERVAL 24 HOUR) and NOW()";
I'm trying to take data from three different tables and output it using as few queries and as little PHP code as possible.
Listed below are the tables I have and the columns in each (only listing relevant columns).
exp_members (A)
columns: member_id, group_id
exp_brandrelations (B)
columns: member_id, brand_id
exp_du_mktgmats (C)
columns: du_id, brand_id, date
I want to loop through the members who belong to group_id='5' (from A), determine which brands are assigned to each member (from B), and get a list of du_ids (from C) that correspond to each member, that have been INSERTed in the last 24 hours.
So far, I can get a list of members in group 5:
SELECT member_id, brand_id FROM exp_brandrelations
WHERE member_id IN (SELECT member_id FROM exp_members where group_id = 5)
And I can get a list of du_ids from the last 24 hours:
SELECT du_id FROM exp_du_mktgmats
WHERE date >= DATE_SUB(NOW(), INTERVAL 1 DAY)
But I'm not sure how best to tie it all together.
This should do it!
SELECT m.member_id, b.brand_id, d.du_id FROM exp_members m, exp_brandrelations b, exp_du_mktgmats d WHERE m.group_id = '5' AND m.member_id = b.member_id AND b.brand_id = d.brand_id AND d.date >= DATE_SUB(NOW(), INTERVAL 1 DAY)
SELECT du_id FROM exp_members m, exp_brandrelations r, exp_du_mktgmats a
WHERE a.brand_id=r.brand_id AND r.member_id=m.member_id
AND date >= DATE_SUB(NOW(), INTERVAL 1 DAY)
AND m.group_id='5'
SELECT
c.du_id
FROM
exp_du_mktgmats c
LEFT JOIN
exp_brandrelations b
ON
c.brand_id = b.brand_id
LEFT JOIN
exp_members a
ON
b.member_id = a.member_id
WHERE
a.group_id = 5
AND
c.date >= DATE_SUB(NOW(), INTERVAL 1 DAY);