how to perform inner join on same table in mysql - mysql

i have table called sla in mysql, i need to get unique switch and port_no related to perticular switch. i exactly don't know which join will help me.
+-------------------------+---------+
| switch | port_no |
+-------------------------+---------+
| 00:00:00:00:00:00:00:02 | 3 |
| 00:00:00:00:00:00:00:01 | 2 |
| 00:00:00:00:00:00:00:01 | 1 |
| 00:00:00:00:00:00:00:02 | 1 |
| 00:00:00:00:00:00:00:04 | 2 |
Expected output
+-------------------------+---------+
| switch | port_no |
+-------------------------+---------+
| 00:00:00:00:00:00:00:02 | 3,1 |
| 00:00:00:00:00:00:00:01 | 2,1 |
| 00:00:00:00:00:00:00:04 | 2 |

I got expected output by using GROUP_CONCAT function.
select switch,GROUP_CONCAT(port_no) from sla group by switch;

Related

mysql return one row of right table

I am facing a huge problem with MYSQL.
I have a table called tperson with the following content
+--------------+------------+
| tperson_id | first_name |
+--------------+------------+
| 1 | juan |
| 2 | miguel |
| 3 | Carlos |
| 4 | Diego |
+--------------+------------+
on the second table i have this data
+--------------+------------+------------+
| tperson_id | trans_code | date_added |
+--------------+------------+------------+
| 1 | 2000-01 |2020/03/03 |
| 1 | 2000-02 |2020/03/04 |
| 2 | 1999-05 |2019/12/25 |
| 3 | 1999-06 |2019/12/26 |
| 3 | 1999-07 |2019/12/27 |
+--------------+------------+------------+
Now I want to have this result in mysql
+--------------+------------+------------+------------+
| tperson_id | first_name | trans_code | date_added |
+--------------+------------+------------+------------+
| 1 | juan |2000-02 | 2020/03/04 |
| 2 | miguel |1999-05 | 2019/12/25 |
| 3 | Carlos |1999-07 | 2019/12/27 |
| 4 | Diego | null | null |
+--------------+------------+------------+------------+
what is the right MYsql statement to generation the result I want?
pls anyone help, I keep looking for the answer found nowhere. I am not good in any database.
thank you so much
I'm assuming your 2nd table name is tdate, and data on trans_code and date_added that's being selected is the latest value if there are more than one data from the same tperson_id on table tdate
SELECT tp.tperson_id, tp.first_name, MAX(td.trans_code), MAX(td.date_added)
FROM tperson tp
LEFT JOIN tdate td
ON tp.tperson_id = td.tperson_id
GROUP BY tp.tperson_id

Query on multiple tables identical tables with different values Mysql

I am in need of some help with mysql since I'm a novice with it. I need to count the values of different tables from different databases and put them all in one table, after that make an index for each count, for example:
_______________________
| | |
| Indexes | Counts |
|___________|___________|
| | |
| Index 1 | Count 1 |
|___________|___________|
| | |
| Index 2 | Count 2 |
|___________|___________|
| | |
| Index 3 | Count 3 |
|___________|___________|
| | |
| Index 4 | Count 4 |
|___________|___________|
But like I'm doing it right now (with UNION) it appears like:
___________
| |
| Counts |
|___________|
| |
| Count 1 |
|___________|
| |
| Count 2 |
|___________|
| |
| Count 3 |
|___________|
| |
| Count 4 |
|___________|
Any idea? Thanks in advance.
EDIT:
The code I'm currently using is this
SELECT COUNT(*) AS "db1" FROM `db1`.`tb1`
UNION
SELECT COUNT(*) AS "db2" FROM `db2`.`tb2`
Try this.
SELECT "db1.tb1" as "INDEXES", COUNT(*) AS "COUNTS" FROM `db1`.`tb1`
UNION
SELECT "db2.tb2" as "INDEXES", COUNT(*) AS "COUNTS" FROM `db2`.`tb2`

How to do a ORDER BY inside a INNER JOIN to get last status update?

I need to get records of 2 tables using filters and sorting.
Tables: ra_pedidos, ra_pedido_has_pedido_status
It worked perfectly in MySQL5.0, but now in 5.5 it does not bring the correct result anymore.
I need in the same line to bring the date of the first status and last change of status of each request and ignore the canceled ones.
Example search (ra_pedido_has_pedido_status):
SELECT id_pedido, id_pedido_status, data
FROM ra_pedido_has_pedido_status
WHERE id_pedido IN (86291, 86745, 86622);
Result:
+-----------+------------------+---------------------+
| id_pedido | id_pedido_status | data |
+-----------+------------------+---------------------+
| 86291 | 1 | 2017-10-30 11:16:12 |
| 86291 | 2 | 2017-10-30 14:14:53 |
| 86291 | 3 | 2017-10-31 08:18:47 |
| 86291 | 11 | 2017-11-07 12:08:04 |
| 86622 | 1 | 2017-11-04 12:23:21 |
| 86622 | 2 | 2017-11-04 12:47:33 |
| 86622 | 3 | 2017-11-06 08:24:20 |
| 86622 | 90 | 2017-11-07 08:40:55 |
| 86745 | 1 | 2017-11-07 10:59:51 |
| 86745 | 2 | 2017-11-07 11:09:46 |
+-----------+------------------+---------------------+
Now report SQL:
[EDIT] Explain: JOIN get first match row, then the ORDER BY on SELECT inside this join force to get last row. Note that id_pedido 86622 should not list because it is canceled (last id_pedido_status = 90), and this is filtered in HAVING
SELECT o.id_pedido AS id, pus.id_pedido_status, pus.data
FROM ra_pedido AS o
INNER JOIN
(
SELECT ra_pedido_has_pedido_status.*
FROM ra_pedido_has_pedido_status
ORDER BY data DESC
) AS pus ON o.id_pedido = pus.id_pedido
WHERE o.id_pedido IN (86291, 86745, 86622)
GROUP BY o.id_pedido
HAVING id_pedido_status < 90
ORDER BY data DESC;
This is the result using MySQL 5.0, the expected:
+-------+------------------+---------------------+
| id | id_pedido_status | data |
+-------+------------------+---------------------+
| 86291 | 11 | 2017-11-07 12:08:04 |
| 86745 | 2 | 2017-11-07 11:09:46 |
+-------+------------------+---------------------+
And this is the result using MySQL 5.5, not expected:
+-------+------------------+---------------------+
| id | id_pedido_status | data |
+-------+------------------+---------------------+
| 86745 | 1 | 2017-11-07 10:59:51 |
| 86622 | 1 | 2017-11-04 12:23:21 |
| 86291 | 1 | 2017-10-30 11:16:12 |
+-------+------------------+---------------------+
What do I need to change in SQL?
Thanks and sorry my english...

SQL INSERT INTO query syntax

I am trying to run an MySQL query to copy over data from an old table (ps__product_review/rate) to a new table (ps_product_comment/grade) based on review ID (id_product_comment). But I am a bit lost on the SQL query, this is what I have but keep getting errors.
INSERT INTO ps_product_comment [(grade)]
SELECT rate
FROM ps__product_review
[WHERE ps__product_review.id_product_comment=ps_product_comment.id_product_comment];
Can anyone help write the correct query?
Edit:Essentially I am trying to populate the Grade column in the new table below.
Old table (ps__product_review)
+--------------------+----------+-----+
| id_product_comment | Comment | Rate|
+--------------------+----------+-----+
| 1 | Good | 2 |
| 2 | Great | 5 |
| 3 | OK | 3 |
| 4 | Brill | 4 |
| 5 | OK | 3 |
| 6 | Average | 2 |
| 7 | Bad | 1 |
+--------------------+----------+-----+
New Table (ps_product_comment)
+--------------------+----------+-------+
| id_product_comment | Comment | Grade |
+--------------------+----------+-------+
| 1 | Good | |
| 2 | Great | |
| 3 | OK | |
| 4 | Brill | |
| 5 | OK | |
| 6 | Average | |
| 7 | Bad | |
+--------------------+----------+-------+
If you want to update table with data from another table, use UPDATE with JOIN
UPDATE ps_product_comment
JOIN ps__product_review
ON ps__product_review.id_product_comment = ps_product_comment.id_product_comment
SET ps_product_comment.grade = ps__product_review.rate;
Remove the square brackets and I think you are missing the JOIN(since you are using that in your where clause):
INSERT INTO ps_product_comment (grade)
SELECT rate
FROM ps__product_review inner join ps_product_comment on
ps__product_review.id_product_comment=ps_product_comment.id_product_comment;

Subtract values from line above the current line in MySQL

I've the following table:
| id | Name | Date of Birth | Date of Death | Result |
| 1 | John | 3546565 | 3548987 | |
| 2 | Mary | 5233654 | 5265458 | |
| 3 | Lewis| 6546876 | 6548752 | |
| 4 | Mark | 6546546 | 6767767 | |
| 5 | Steve| 6546877 | 6548798 | |
And I need to do this for the whole table:
Result = 1, if( current_row(Date of Birth) - row_above_current_row(Date of Death))>X else 0
To make things easier, I guess, I created the same table above but with 2 extra id fields: id_minus_one and id_plus_one
Like this:
| id | id_minus_one | id_plus_one |Name | Date_of_Birth | Date_of_Death | Result |
| 1 | 0 | 2 |John | 3546565 | 3548987 | |
| 2 | 1 | 3 |Mary | 5233654 | 5265458 | |
| 3 | 2 | 4 |Lewis| 6546876 | 6548752 | |
| 4 | 3 | 5 |Mark | 6546546 | 6767767 | |
| 5 | 4 | 6 |Steve| 6546877 | 6548798 | |
So my approach would be something like (in pseudo code):
for id=1, ignore result. (Because there is no row above)
for id=2, Result = 1 if( (Where id=2).Date_of_Birth - (where id_minus_one=id-1).Date_of_Death )>X else 0
for id=3, Result = 1 if( (Where id=3).Date_of_Birth - (where id_minus_one=id-1).Date_of_Death)>X else 0
and so on for the whole table...
Just ignore id_plus_one if there is no need for it, I'll use it later for the same thing. So, if I manage to do this for id_minus_one I'll manage for id_plus_one as they are the same algorithm.
My question is how to pass that pseudo code into SQL code, I can't find a way to relate both ids in just one select.
Thank you!
As you describe this, it is just a self join with some logic on the select:
select t.*,
((t.date_of_birth - tprev.date_of_death) > x) as flag
from t left outer join
t tprev
on t.id_minus_one = tprev.id