mysql:select and insert from same table - mysql

Need suggestion.
And, I've 2 tables like master and slave.
master contains 3 columns following as:
searchTerm | group |campaign
------------------------------
apple macbook | null |null
apple ipad | null |null
And slave contains 2 columns and multiple entries following as:
group | campaign
----------------
apple | Laptops
Note: if apple is found in serachterm of master table and then group and campaign should be inserted in the master table and run in joins for all slave tables entry

Try Like This
insert into slave (group,campaign) select group campaingn from maseter
seachterm=your search term

in mysql
update master m
inner join slave s
on m.searchterm like concat('%',s.group,'%')
set m.group = s.group,
m.campaign = s.campaign;

her this one will be going to solve you problem
select * from master;
select * from salve;
SELECT m.id,m.sterm,s.grop,s.camp
FROM master m
INNER JOIN salve s
ON m.id=s.id;
you should refer this link

Related

mysql trigger one to many relation

So i have 3 tables Production, Stop_Prodcution and triggered_table.
production has a one to many realation with Stop_prodcution where a production can have a lot of stop prodcutions.
production table
-----------------------
id_prod | date
-----------------------
1 |20/03/2019
2 |18/04/2019
Stop_Production table
----------------------------
id_stop | name | id_prod
----------------------------
1 | Any reason | 1
2 | Lunch | 1
3 |damaged prod| 2
triggered_table
----------------------------
id|id_prod|date|id_stop|name
i've created 2 triggers:
after insert into production
for each row
insert into triggered_table
(id_prod,date) values (new.id_prod, curdate())
and the other one:
after update
set id_stop=new.id_stop,
name= new.name
where id_prod= new.id_prod
the problem is that a production record is able to have 2 or more stop_Production records so with the triggers that I have, it will update always the same record, but what I need is a new record with same information of production table and the information that differs from the new inserted row in stop_production, please tell me if I explained my self if not I'll try to be more clear.
This query will give you the results you want, without using a trigger:
SELECT
t1.id_stop,
t1.id_prod,
t1.`name`,
t2.date
FROM stop_production
LEFT JOIN production
ON (t1.id_prod = t2.id);
If you want to make a "table" out of this, you can create a view.
CREATE VIEW triggered_table AS (
SELECT
t1.id_stop,
t1.id_prod,
t1.`name`,
t2.date
FROM stop_production
LEFT JOIN production
ON (t1.id_prod = t2.id)
)
Then, if you want to SELECTfrom this "table", you can simply:
SELECT * FROM triggered_table;

MySQL special case pivot

I cant find an answer to this despite looking for several days!
In MySQL I have 2 Tables
ProcessList contains foreign keys all from the process Table
ID |Operation1|Operation2|Operation3|etc....
---------------------------------------
1 | 1 | 4 | 6 | ....
---------------------------------------
2 | 2 | 4 | 5 |....
---------------------------------------
.
.
.
Process Table
ID | Name
-------------------
1 | Quote
2 | Order
3 | On-Hold
4 | Manufacturing
5 | Final Inpection
6 | Complete
Now, I am new to SQL but I understand that MYSQL doesnt have a pivot function as Ive researched, and I see some examples with UNIONs etc, but I need an SQL expression something like (pseudocode)
SELECT name FROM process
(IF process.id APPEARS in a row of the ProcessList)
WHERE processListID = 2
so I get the result
Order
Manufacturing
Final Inspection
I really need the last line of the query to be
WHERE processListID = ?
because otherwise I will have to completely rewrite my app as the SQL is stored in a String in java, and the app suplies the key index only at the end of the statement.
One option is using union to unpivot the processlist table and joining it to the process table.
select p.name
from process p
join (select id,operation1 as operation from processlist
union all
select id,operation2 from processlist
union all
select id,operation3 from processlist
--add more unions as needed based on the number of operations
) pl
on pl.operation=p.id
where pl.id = ?
If you always consider only a single line in the process list (i.e. procsessListId = x), the following query should do a pretty simple and performant job:
select p.name from process p, list l
where l.id = 2
and (p.id in (l.operation1, l.operation2, l.operation3))

Database design for getting unread article in mysql (over 100M row table)

This information is very condensed.
There are 2 tables.
article
-----------------------------------
|id | weight | text |
-----------------------------------
|1 | 10 | blah |
|2 | 100 | blah |
|3 | 50 | blah |
|4 | 1000 | blah |
-----------------------------------
read
-----------------------------------
| user_id | article_id |
-----------------------------------
| 1 | 4 |
| 1 | 2 |
| 1 | 3 |
| 2 | 3 |
| 2 | 4 |
-----------------------------------
I want to get unread articles using below query (very condensed)
SELECT
a.*
FROM
article a LEFT OUTER JOIN read r ON r.article_id = a.id and r.user_id = 1
WHERE
r.id IS NULL
ORDER BY
a.weight DESC
LIMIT 10
important information
the number of read table rows keeps under 1000 per user. (remove old data)
weight column in article table is changed frequently. (It means order not fixed)
problem is .. (when number of users : over 1M)
the way to get unread articles using read table (not in, outer join is not important)
number of read table rows will be over 1G
It works well so far (current # of read table rows : 100M). but I have to prepare next step because number of users is increasing rapidly.
What is the best way for large service in this case?
(sharding? partitioning table? or redesign architecture?)
Thanks in advance
Add a column to article. It will be a flag saying whether the article is read/unread. (Do not make it a user count or a timestamp; that will slowdown the subsequent steps.)
Whenever a user reads an article, check the flag and change it if needed.
Have `INDEX(flag, weight, id) -- this will let your query run almost instantly. This should be OK on that million-row table.
A problem: Since you are purging (after 1000), some "read" articles can become "unread". To deal with this, batch the purging, and gather the distinct list of articles that got purged. Then do the tedious task of re-computing the flag, but just for those articles. INDEX(article_id) will help; use EXISTS ( SELECT * FROM read WHERE article_id = $aid ). (This can probably be turned into a batch operation rather than one aid at a time.)
Another problem: secondary keys on billion-row tables are costly -- they may lead to a lot of I/O. Before attempting to address this problem, please provide SHOW CREATE TABLE for both tables, plus any other common SELECTs. Picking the right index(es) and datatypes is very important to performance in billion-row tables..
Point is, to use the index as far as possible.
SELECT a.*
FROM a
LEFT JOIN read r
ON r.article_id = a.id and r.user_id =1
WHERE r.id IS NULL
ORDER BY a.weight DESC
LIMIT 10
Edit:
The concern for you is the data size of read table and we have to reduce the data size. For that we have multiple options:
MySQL partitions: create partitions on range of user_id ( may be 100K users per partition
Create multiple tables: Similar to partitioning, but you will have the data in different databases(even in different DB servers). Based on the user_id, you will decide on the table/database to join.
Also, you can think of having archival of old data periodically and the application should be smart enough to decide on whether it needs to query archived tables or live table.

MySql Select on two servers

How can this be possible on two servers? I m using MySQL and c#.net, the Insert is done perfectly but now I don't know how to do select!
server: 127.0.0.1
tbl_student
roll_no| stu_name
1 | abc
2 | def
3 | xyz
Server:127.0.0.2
tbl_room
room_id| room_name
1 | A1
2 | A2
3 | A3
tbl_info (on server:127.0.0.2)
id | roll_no | room_id
1 | 1 |2
2 | 2 |3
3 | 3 |3
select i.id, i.roll_no, s.stu_name, r.room_name
from tbl_student as s, tbl_room as r, tbl_info as i
where i.roll_no = s.roll_no and i.room_id = r.room_id
I don't know which version you are using. Try to make a research on DB-Link. This is the term used to what you need.
In a quick research I saw that is a openend ticket on mysql dev group:
http://dev.mysql.com/worklog/task/?id=1150
You need FEDERATED Storage Engine for link one table in the second server to the first.
If your main server is: 127.0.0.2
you can mapped the table tbl_student present in the server 127.0.0.1
in the other server, before you need to create a mirror table (pseudo code):
CREATE TABLE `tbl_student `(`roll_no` Int, stu_name VARCHAR(100))) ENGINE=FEDERATED
CONNECTION='MYSQL://127.0.0.1:3306/dbname/tbl_student ';
Now you can operate only in the main server.
The FEDERATED storage engine supports SELECT, INSERT, UPDATE, DELETE, and indexes. It does not support ALTER TABLE, or any Data Definition Language statements that directly affect the structure of the table, other than DROP TABLE. The current implementation does not use prepared statements.
Performance on a FEDERATED table is slower.
For more info:
http://dev.mysql.com/doc/refman/5.0/en/federated-use.html
I hope you helpful

Merge 2 databases into another one without duplicate records

I have a question I want to merge 2 databases into another one but the problem is there are duplicate rows. So how can I merge the 2 databases into another one without duplicate records? I hope someone can help me.
Database 1
Name | Number | Code
Demo | 0293 | 827
Test | 0482 | 923
Database 2
Name | Number | Code
Lol | 0223 | 182
Stack| 1923 | 829
Demo | 0293 | 827
Thanks
This should really be done with a unique key and ON DUPLICATE KEY. If not willing to do that, the following would also work:
Assuming the tables are identical and that you only want to exclude a row when ALL columns are the same as a row in the target table.
INSERT into Table1
SELECT * from Table2
WHERE concat(Name,'|',Number,'|',Code) not in (
SELECT concat(Name,'|',Number,'|',Code)
FROM Table1
)
Select A.*,B.* from TABLE1 AS A,TABLE2 AS B where A.code=B.code GROUP BY A.code;
I think you are talking about TABLE not DATABASE. Use GROUP BY clause or find DISTINCT value with the query. And you have not specified any primary key..so read mysql tutorial first..