I can code in PHP but I'm not good with SQL at all. I need to run an update on a table in order to pass in a given user_id and set the "access_end" date for all products the user owns to one year from today's date.
Any help much appreciated
Database is MySQL
Table name is dap_users_products_jn
Relevant Fields in database are:
user_id | access_end_date | product_id
1 | 2012-10-26 | 34
1 | 2012-11-21 | 30
1 | 2012-12-22 | 3
2 | 2012-10-20 | 34
2 | 2012-07-18 | 30
2 | 2012-08-15 | 3
...etc
update dap_users_products_jn
set access_end_date = date_add(now(), interval 1 year)
where user_id = 1
Related
I have 6 tables to store my user data
tbl_user_name
uuid | username | date (unix time)
-----------------------------------------
0 | lisha.s | 1489857236
1 | titami | 1485853232
2 | mathew | 1442853636 <----|> Users can change their username
3 | sandarjun | 1489857239 |> so i need to get the uuid by the
2 | mathew_kar | 1575456274 <----|> newest username which is given
tbl_user_fullname:
uuid | fullname | date (unix time)
-----------------------------------------
0 | Lisha Simonette | 1489857236
1 | Titus Amiran | 1481256345
2 | Mathew Karolina | 1489234455
3 | Sandhya Arjun | 1489857239
0 | Lisha Karolina | 1599999999
tbl_user_website:
uuid | website | date (unix time)
-----------------------------------------
0 | google.com | 1489857236
1 | titusamiran.com | 1489855234
2 | mathewk.net | 1489857432
3 | blod.sandhya.info | 1489857239
tbl_user_birthdate:
uuid | birthdate | date (unix time)
-----------------------------------------
0 | 02-05-1991 | 1489857236
1 | 05-08-1980 | 1489857123
2 | 09-09-1992 | 1489851334
3 | 17-02-1998 | 1489857239
tbl_user_follower:
uuid | follower_uuid
-----------------------
0 | 4
1 | 8
2 | 0
3 | 4
3 | 2
3 | 1
tbl_user_online:
uuid | last_seen (unix time)
-----------------------
0 | 1489855334
1 | 1589851111
2 | 1689857234
3 | 1789834539
i want to collect the uuid, the fullname, the website, the birthdate and the number of followers by a user with a given username. (i need to get the newest uuid of a username because the user can change the username).
The date column is the timestamp when they changes a value. for example in tbl_user_fullname: Lisha Simonette (uuid 0) married Mathew Karolina (uuid 2) so i neet to get the new fullname of Lisha (uuid 0) by the date column. And so on ... for tbl_user_website and tbl_user_birthdate .. even if they dont change their birthday often ;)
From the table tbl_user_online i only need the last_seen timestamp.
The value i give to the query is the username. the username should give out the uuid with which i can query the other tables.
Thank you very much for your help and sorry for my bad english ;)
The below query will resolve the question asked:
SELECT usr.uuid,
ful.fullname,
web.website,
brt.birthdate,
COUNT(fol.follower_uuid)
FROM tbl_user_name usr
JOIN tbl_full_name ful ON ful.uuid = usr.uuid
LEFT JOIN
tbl_full_name ful2 ON ful2.uuid = ful.uuid
AND ful2.time_stamp > ful.time_stamp
JOIN tbl_user_website web ON web.uuid = usr.uuid
LEFT JOIN
tbl_user_website web2 ON web2.uuid = web.uuid
AND web2.time_stamp > web.time_stamp
JOIN tbl_user_birthdate brt ON brt.uuid = usr.uuid
LEFT JOIN
tbl_user_birthdate brt2 ON brt2.uuid = brt.uuid
AND brt2.time_stamp > brt.time_stamp
JOIN tbl_user_follower fol ON fol.uuid = usr.uuid
WHERE usr.username = 'jack_2'
AND ful2.uuid IS NULL
AND web2.uuid IS NULL
AND brt2.uuid IS NULL
GROUP BY
usr.uuid,
ful.fullname,
web.website,
brt.birthdate
This query works by isolating the latest timestamp in a table that shares a common element in a row. In this case the uuid.
Here is the fiddle to see it working.
Here is a fiddle without the username filter
You have a bigger issue that you need to resolve, however. You should not be storing values like you are with timestamps.
I suggest you look into table triggers for update and insert statements. You should have a trigger set up to automatically insert an entry into an audit table that stores this information while keeping your core functioning tables small and orderly.
e.g create a table called tbl_user_name_audit. Have a trigger on tbl_user_name for updates. When a username is updated the previous value is inserted into the tbl_user_name_audit table with a before and after value as well as a timestamp and audit type (INSERT/UPDATE/DELETE).
I have a table with multiple rows for each customer and and a visit_date. The visit date can be null as well.
My tables are as below:
customers:
id | name | email
1 | John Doe1 | a.b#gmail.com
2 | John Doe2 | b.c#gmail.com
3 | John Doe3 | x.y#gmail.com
store_customers
id | customer_id | store_id | email_optedin | visit_date
1 | 1 | 1 | 1 | 2015-11-30
2 | 1 | 2 | 1 | 2016-05-08
3 | 1 | 3 | 1 | null
4 | 2 | 1 | 1 | 2015-04-30
5 | 2 | 2 | 1 | 2015-08-40
6 | 2 | 3 | 1 | 2015-12-12
7 | 3 | 1 | 1 | null
8 | 3 | 2 | 1 | null
9 | 3 | 3 | 1 | null
I am trying to retrieve customers who either have not had a visit to the any of the three stores or have not visited since a specified date (e.g. 2016-04-15).
I am expecting customers 2 and 3 but not 1.
I tried this query:
select distinct * from customers
inner join store_customers on store_customers.customer_id = customers.id
where customers.email != '' and
store_customer.store_id in (1,2,3) and customers.emailStatus not in ('Unverified','Bounced','Spammed')
and
(
store_customer.email_optedin = 1
and max(store_customers.visit_date) <= '2016-04-15'
or account_customer.visit_date is null
);
This does not work. I somehow need to, for the set of store ids), I need to select customers who have either not had any visit (all nulls for visit date for the specified stores) or the if one or more visit dates are available then compare the max date to the specified date.
I found similar questions but none of the answers has worked for me, mainly because of the requirement of selecting either those customers who have no visit or if they do atleast one, then to compare the latest visit date from the set of stores in the joined table.
I am trying to do this all in one query but if that is not possible then I can break it up as well. I also do not want to change the order of joins because there are many other things added on to this query and changing the order of joins may become a problem.
I really appreciate any help that can be provided.
Regards,
Waqar
Try this query
SELECT
customers.id,
customers.name,
MAX(store_customers.visit_date)
FROM
customers LEFT JOIN store_customers on customers.id = store_customers.customer_id
GROUP BY customers.id,customers.name
HAVING MAX(store_customers.visit_date) < '2016-04-15' OR MAX(store_customers.visit_date) IS NULL
Okay guys I have big problem with mySQL. I need to to do select that gets the free tables in the restaurant in specific time. Here are my tables:
tables:
+-------------+
| id | chairs|
+-------------+
| 1 | 3 |
| 2 | 5 |
| 4 | 10 |
| 7 | 12 |
| 10 | 6 |
+-------------+
and reservations:
+---------------------------------+
| id | table_id | start_datetime |
+----------------|---------------------+
| 1 | 3 | 2013-11-27 16:15:00 |
| 2 | 5 | 2013-11-27 19:00:00 |
+----------------|---------------------+
I try something like this
SELECT *
FROM wp_reserveit_tables,wp_reserveit_reservations
WHERE (wp_reserveit_tables.chairs = wp_reserveit_reservations.table_id
AND wp_reserveit_reservations.start_datetime - INTERVAL 1 HOUR >= '2013-11-29 16:15:00'
AND wp_reserveit_reservations.start_datetime + INTERVAL 1 HOUR <= '2013-11-29 16:15:00')
OR wp_reserveit_tables.chairs <> wp_reserveit_reservations.table_id
but it gives me SQL error.
So please if you have and idea please write it down.
Thanks
Reservation table will have entry only if there are any reservations. So we can do it simply with NOT IN clause I guess,
SELECT * FROM wp_reserveit_tables WHERE chairs_id NOT IN
(SELECT table_id FROM wp_reserveit_reservations GROUP BY table_id)
In your query you have joined them with chairs column with table_id column. So am not sure which one matches. So please alter the column in Where clause as per your requirement.
You have column name start_datetime in your query and your table structure specifies it to be starttime.
Unless you specify further, this does look like a valid error if it is not a typo.
I need to write a query to retrieve values from two columns using mysql table
My table has the following strucutre
| ID | user_id | datetime | message |
| 1 | 21 | 2012-05-10 04:13:01 | message1 |
| 2 | 07 | 2012-05-10 04:17:51 | message2 |
| 3 | 21 | 2012-05-11 04:21:51 | message3 |
| 4 | 21 | 2012-05-11 04:43:51 | message4 |
| 5 | 07 | 2012-05-11 04:21:51 | message5 |
| 5 | 21 | 2012-05-11 04:43:51 | message6 |
i wrote the below query
$query="SELECT MAX(datetime) FROM messages where user_id=21 and date=2012-05-11";
but i am not getting latest record from table iam getting null value
help us
$query="SELECT MAX(datetime) FROM messages where user_id=21 and date LIKE '2012-05-11%'";
You should use DATE(date) to get date of timestamp. MySQL function DATE() extracts only date without hours, minutes and seconds.
Here is your query:
SELECT MAX(datetime)
FROM messages
WHERE user_id = 21 AND DATE(date) = '2012-05-11'
Have you tried the following?
$query="SELECT MAX(datetime) FROM messages where user_id=21";
Update:
In your question, you didn't specify if you wanted to retrieve last record for a certain date. If you do, you'd need to use MySQL's date function.
Are you looking for the most recent record? You can get this with a subquery:
$query = "SELECT * FROM messages WHERE datetime = (SELECT MAX(datetime) FROM messages)";
Your query asks for ".....date=2012-05-11";" but your table does not have the field named date? did you mean datetime? if so, you may want to try ".....datetime like '2012-05-11%'";
MYSQL Table trial_list structure as follows...
id | product_id | expiry_date(date) | by_user | curr_datentime(timestamp)
we are able to extend any trial, and if we do that it simply another row with new expiry_date.
Now we would like to get rows got expired yesterday, we are currently using following sql query.....
Sample MYSQL DATASET
+----+-------------+-------------+-------------+----------+---------------------+
| id | product_id | comment | expiry_date | by_user | dnt |
+----+-------------+-------------+-------------+----------+---------------------+
| 2 | 50 | testing | 2011-02-18 | tester | 2011-02-17 23:36:12 |
+----+-------------+-------------+-------------+----------+---------------------+
| 3 | 50 | again | 2011-02-20 | tester | 2011-02-19 20:36:12 |
+----+-------------+-------------+-------------+----------+---------------------+
| 4 | 50 | extend | 2011-02-23 | tester | 2011-02-21 22:36:12 |
+----+-------------+-------------+-------------+----------+---------------------+
$sql = 'SELECT id, product_id, expiry_date, by_user, curr_datentime FROM trial_list WHERE expiry_date < CURDATE() ORDER BY expiry_date DESC';
We believe this is not correct as its getting all rows which date is older than yesterday not updated expiry_date, suppose we have given some user expiry date 1st feb 2011 and then we change again with 12th feb 2011, so it selects 1st feb 2011 entry. I think it makes sense.
What you have to do first is get the latest item per product_id. After that you can further filter it down to those which are expired. Something like:
SELECT a.* FROM
trial_list AS a
LEFT JOIN trial_list AS b ON a.product_id = b.product_id AND a.id < b.id
WHERE b.product_id IS NULL
AND a.expiry_date < curdate()
See http://dev.mysql.com/doc/refman/5.5/en/example-maximum-row.html
Try using NOW() instead of CURDATE(), you are comparing a Date to a Timestamp, NOW() will compare timestamps.