Sql query for searching values in the same table Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Sql query for searching values in the same table without wasting too much if your time.

The question is published on by Tutorial Guruji team.

i’ve got a problem. I have a table with these columns:

languageID languageItem

Every row can have only “2” or “3” as languageID and languageItem is always different, but only with the same languageID. For example i can have:

2 | header.title
2 | header.description
3 | header.description
3 | header.title

The problem is that now the rows which have the languageID as “3” are less than rows which have the languageID as “2” and i need that for each languageID there must be the same languageItem(s). Like:

2 | header.title
2 | header.description
2 | header.button
3 | header.title
3 | header.description

Is missing header.button for “3”

I want select all rows which the languageID 2 have more and then “copy” them and insert with the languageID 3.

Thanks

EDIT: The rows don’t have only these 2 columns, but also others.

Answer

You can do this by just inserting values that don’t exist. The query looks like:

insert into table(languageId, languageItem)
    select 3, languageitem
    from table t
    where languageid = 2 and
          not exists (select 1
                      from table t3
                      where t3.languageid = 3 and t3.languageitem = t.langaugeitem
                     );
We are here to answer your question about Sql query for searching values in the same table - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji