Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6190185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:32:31+00:00 2026-05-24T02:32:31+00:00

I have two tables table1 and table2. Table1 is generated by a parser and

  • 0

I have two tables table1 and table2. Table1 is generated by a parser and automatically updated every few days. Table2 is the user edited version of table1. If a record exist in table2, that should overlay the record in table1 in the view.

Any edited version in table2 has the original content from table1 for Column2 and Column3 in OldColumn2 and OldColumn3 respectively. When a user deletes a record, the deleted column has value 1 and if the user wants to add the record again it has value 0 to display in the view. By default the value is NULL if it’s never been deleted.

If an new record in inserted into table2, OldColumn2 and OldColumn3 has the value new string to distinguish that the record doesn’t exist in table1.

Here is the design of my table.

Table1
+------+------+------+-------------+
| Col1 | Col2 | Col3 | OtherColumns|
+------+------+------+-------------+
|   a1 |   b1 |   c1 |    Data     |
+------+------+------+-------------+
|   a2 |   b2 |   c2 |    Data     |
+------+------+------+-------------+
|   a3 |   b3 |   c3 |    Data     |
+------+------+------+-------------+
|   a4 |   b4 |   c4 |    Data     |
+------+------+------+-------------+


Table2
+------+------+------+-------------+----------+----------+---------+
| Col1 | Col2 | Col3 | OtherColumns| OldCol2  |  OldCol3 | Deleted |
+------+------+------+-------------+----------+----------+---------+
|  a1  |   e1 |  f1  |   Data      |    b1    |     c1   |   NULL  |
+------+------+------+-------------+----------+----------+---------+
|  a2  |   k2 |  m2  |   Data      |    b2    |     c2   |   0     |
+------+------+------+-------------+----------+----------+---------+
|  a3  |   k3 |  m3  |   Data      |    b3    |     c3   |   1     |
+------+------+------+-------------+----------+----------+---------+
|  z1  |   kk |  jj  |   Data      |   new    |   new    |   1     |
+------+------+------+-------------+----------+----------+---------+
|  z2  |   kj |  uu  |   Data      |   new    |   new    |   0     |
+------+------+------+-------------+----------+----------+---------+

    View
+------+------+------+-------------+----------+----------+---------+
| Col1 | Col2 | Col3 | OtherColumns| OldCol2  |  OldCol3 | Deleted |
+------+------+------+-------------+----------+----------+---------+
|  a1  |   e1 |  f1  |   Data      |    b1    |     c1   |   NULL  |
+------+------+------+-------------+----------+----------+---------+
|  a2  |   k2 |  m2  |   Data      |    b2    |     c2   |   0     |//Deleted then added
+------+------+------+-------------+----------+----------+---------+
|  a4  |   k4 |  j4  |   Data      |    NULL  |     NULL |   0     |
+------+------+------+-------------+----------+----------+---------+
|  z2  |   kj |  uu  |   Data      |   new    |   new    |   0     |
+------+------+------+-------------+----------+----------+---------+

Here is my view statement.

CREATE VIEW NEWVIEW
AS
SELECT t2.* FROM table1 t1
LEFT JOIN table2 t2
ON t1.column1 = t2.column1 AND t1.column2 = t2.oldColumn2 AND t1.column3 = t2.oldColumn3
WHERE t2.column1 IS NOT NULL AND t2.Deleted = 0

UNION

SELECT t1.*, null, null, null FROM table1 t1
LEFT JOIN table2 t2
ON t1.column1 = t2.column1 AND t1.column2 = t2.oldColumn2 AND t1.column3 = t2.oldColumn3
WHERE t2.column1 IS NULL

UNION

SELECT * FROM table2 WHERE oldColumn2 = 'new' AND oldColumn3 = 'new' AND Deleted = 0

The view is a bit slow right now. How can I optimize this view?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:32:32+00:00Added an answer on May 24, 2026 at 2:32 am

    Try to change first two SELECT statements in your view with this one –

    SELECT
      t1.column1,
      t1.column2,
      t1.column3,
      IF(t2.column1 IS NULL, t1.OtherColumns, t2.OtherColumns) OtherColumns,
      IF(t2.column1 IS NULL, NULL, t2.OldCol2) OldCol2,
      IF(t2.column1 IS NULL, NULL, t2.OldCol3) OldCol3,
      IF(t2.column1 IS NULL, NULL, t2.Deleted) Deleted
    FROM
      table1 t1
    LEFT JOIN table2 t2
      ON t1.column1 = t2.column1 AND t1.column2 = t2.oldColumn2 AND t1.column3 = t2.oldColumn3
    WHERE 
      t2.column1 IS NULL OR t2.column1 IS NOT NULL AND t2.Deleted = 0
    

    Note, than views may have bad performance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables: table1, table2. Table1 has 10 columns, table2 has 2 columns.
I have two tables (TABLE1, TABLE2 - unique i know) that has a 1-to-many
I have a database with two tables ( Table1 and Table2 ). They both
i have two tables table1 fields fid,fname,fage a ,abc ,20 b ,bcv ,21 c
I have the following two tables: Table1 ---------- ID Name 1 A 2 B
I have two tables with the following columns: table1: id, agent_name, ticket_id, category, date_logged
Let's say I have two tables with several fields and in every table there
I'm using table adapters and datasets in .NET (version 2.0). I have two tables
I have two tables: Users and Roles. A user may have more roles, so
I have two tables that need to be updated, Master and Identifiers: Master --MasterID

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.