Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

codemode's avatar

Need help with SQL query - stuck since days

Hello,

My question is more about a SQL query, rather than Laravel.

There is a table called ´item_meta´ which has a foreign key `fk_i_item_ and a field `s_val.

And a table called t_user which has the primary key pk_i_id , and a field s_phone_mobile

And a table called t_item which has the primary key pk_i_id, a foreign key fk_i_user_id

So, t_user has many t_item and an t_item has one item_meta .

I want to transfer the data from the field ´s_value´ of the table `item_me into the field `s_phone_mobi of the table `t_us

The horribly wrong SQL query is as follows :

insert into t_user(pk_i_id, s_phone_mobile)
select fk_i_item_id, s_value from t_item_meta where fk_i_item_id = some connection with the user table

I've been with this since days and days, i would be very content if someone could help me here.

Thanks.

0 likes
9 replies
burlresearch's avatar

What you're describing is not possible.

t_user has many t_item and ant_item has one item_meta

So if this is true, then there are many item_meta.s_value for each t_user.

You see - how do you plan to choose which of those values to set as t_user.phone_mobile?

codemode's avatar

Hi @burlresearch , yes you're right. However, the the field item_meta.s_value will have the same value for each user.

So, in simple words, each time an ITEM is created, for a particular user, the item_meta.s_value will be same each time.

Thank you..

burlresearch's avatar
Level 40

OK - as long as you're sure this is the case, then we can try.

Since you already have the users you don't want to INSERT new rows in t_user - you are looking for an UPDATE here - you just want to put the phone on the existing record.

So that means our query is going to be something like: update t_user SET s_phone_mobile = item_meta.s_value

Now we just have to figure out how to join that item_meta. Since there are many meta rows foreach, we're going to have to pick [any] one. Since they're all the same, it won't matter which.

With MySQL that pretty much means we need a GROUP BY, or LIMIT 1. I prefer grouping:

So then we can get item_meta joined to their t_items and group the results by the user_id - so we only get one:

SELECT *
FROM t_item i
  JOIN item_meta m ON i.pk_i_id = m.fk_i_item_id
GROUP BY fk_i_user_id

You should be able to run that query and see the rows - one per user_id and there will be a single field of m.s_value... that's what we need for our UPDATE. So we're in business, now we can combine them:

UPDATE t_user
SET s_phone_mobile = (
  SELECT m.s_value
  FROM t_item i
    JOIN item_meta m ON i.pk_i_id = m.fk_i_item_id
  WHERE i.fk_i_user_id = t_user.pk_i_id
  GROUP BY fk_i_user_id
)

and you should be good.

1 like
codemode's avatar

Hello @burlresearch , the query ran fine on my local machine, but does not run on the server.

This is the error i see :

Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'DBNAME.m.s_value' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

Any ideas? Thanks.

burlresearch's avatar

You could use a LIMIT 1 variation in the subquery, if the group-by isn't portable. It's an option.

1 like

Please or to participate in this conversation.