0

I have two tables that are related to each other.

One is user_registration and the other is user_group.

Inside the user_registration table, I have the fields:

  • id
  • id_user
  • date_begin
  • date_end

And user_group:

  • id
  • id_user
  • id_group

I have just tried to learn simple join statements to select some records based on their group id like:

SELECT a.id_user, a.date_begin, a.date_end, b.id_group
FROM user_registration as a
INNER JOIN user_group as b
ON a.id_user=b.id_user
WHERE b.id_group = '14'

But right now, what I am trying to do is to set the date_end to be the same as the date_begin where in the id_group of the records will be the same.


I already got it. This did the trick although I am not sure this is the most efficient way as I am still learning MySQL.

UPDATE user_registration AS a
INNER JOIN user_group AS b ON a.id_user = b.id_user
SET a.date_end = a.date_begin
WHERE b.id_group = '14'
4
  • 1
    update queries can use joined tables as data sources.
    – Marc B
    Commented May 11, 2014 at 5:23
  • Can you edit your question to explain what table the date_end and date_begin are compared to the id_group? Looking at the user_group table I do not see a date_begin or date_end so not understanding what you are looking for? Then I see this billing_user_period table but no schema for that?
    – Shawn
    Commented May 11, 2014 at 5:27
  • @Shawn, it was supposed to be user_registration instead of billing_user_period. Please see my edit. I have also tried to create a query but I am having an error.
    – Atasha
    Commented May 11, 2014 at 5:35
  • @drifter that query is correct. You should choose to answer your own question with it so this question is resolved. You can also do all groups by not including the where clause
    – Shawn
    Commented May 11, 2014 at 5:41

1 Answer 1

2

As mentioned on the edit above, this accomplished what I was trying to do.

UPDATE user_registration AS a
INNER JOIN user_group AS b ON a.id_user = b.id_user
SET a.date_end = a.date_begin
WHERE b.id_group = '14'

Assuming that 14 is a group id.

I am not that certain about its efficiency though. I still have to do more readings and learn more about it.

2
  • Click the checkmark so this question is resolved :-)
    – Shawn
    Commented May 11, 2014 at 5:41
  • @Shawn, it says I can only accept my own answer in 2 days. A bit of a bummer but I'm happy I was able to solve it on my own. :)
    – Atasha
    Commented May 11, 2014 at 5:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.