Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write such query?

Tags:

sql

mysql

How do you programming in SQL, I don't understand nothing in it. :)

Table1:

+-----------+----------+------------+
| id        | key      | page       |
+-----------+----------+------------+
| 1         | A        | 123        |
| 2         | B        | 456        |
| 3         | C        | 777        |
+-----------+----------+------------+

Table2:

+-----------+----------+------------+
| id        | key      | page       |
+-----------+----------+------------+
| 1         | A        | 123        |
| 2         | B        | 456        |
| 3         | C        | 111        |
+-----------+----------+------------+

I have 2 tables. We can see that they have fields with the same 'key' (A, B, C). How can I filter 'page' column's value which are differ in Table1 and Table2.

So, in page column: 123=123, 456=456, 777<>111 ==> I want such table as Result:

NEW Table:

+-----------+---------------------+---------------------+
| id        | id & page_table1    | id & page_table2    |
+-----------+---------------------+---------------------+
| 1         | 3   777             | 3    111            |
+-----------+---------------------+---------------------+
like image 989
vppy Avatar asked Dec 07 '25 06:12

vppy


1 Answers

@LukazSzozda's comment seems to be the right answer. But if you actually want exactly those three comments in the result, you can concatenate them with the concat function, like this

select a.id,
       concat(a.id, ' ', a.page)
       concat(b.id, ' ', b.page),
from Table1 a
join Table2 b
     on a.id = b.id
     and a.key = b.key
where a.page != b.page
like image 145
Niles Avatar answered Dec 09 '25 21:12

Niles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!