Mysql multiple simultaneous insert into queries - mysql

I need to insert data into a set of columns in data_table (table 1) from alarm_status_log_table (table 2, the whole thing), and another set of data into data_table (table 1) from channel table (table 3, just name and description), thus completing data_table (table 1).
But the problem is, the name & description columns which I need to pass onto data_table must be on the same row as the alarm_id they correspond with. That alarm_id is the same as the channel_id from the channel table which happens to already have the name and description that I want to send to data_table.
Basically I want to say:
Insert (name, description) from channel(table 3) into data_table(name, description) where channel_id = alarm_id
How can I proceed?

It is possible to make an update JOIN like this:
UPDATE data_table a
INNER JOIN table3 b ON a.alarm_id = b.channel_id
SET a.name=b.name, a.description=b.description
If you do not want to update, can you just join the tables? Maybe it would be better to create a joined view; this would avoid having redundant inconsistent data.

All i needed to do was to use a VIEW, since reordering data coming from two tables into a third table is quite tricky, here is the script i used to get the view i wanted:
SELECT channel.name, channel.description, alarm_status_log.*
FROM data2desk.alarm_status_log
LEFT JOIN data2desk.channel ON channel.channel_id=alarm_status_log.alarm_id
And it did the trick :)
final view

Related

Nested select from two tables in mysql

I have two tables
table A contain many ids, which one is related to the table B.
I have to make a query on the table A to select only some ids and then take the data from the table B using the selected ids from the table A
How can i do that? (MYSQL)
Based on your description, here is a simple structure of how the query might go, although I recommend you share some sample results & the desired output.
SELECT * FROM tableA
JOIN tableB
ON tableA.id_song == tableB.id_song
WHERE "your condition HERE"

How can I combine these two tables so that I can sort with information on each table, but not get duplicate answers?

I have two tables. The first is named master_list. It has these fields: master_id, item_id, name, img, item_code, and length. My second table is named types_join. It has these fields: master_id and type_id. (There is a third table, but it is not being used in the queries. It is more for reference.) I need to be able to combine these two tables so that I can sift the results to only show certain ones but part of the information to sift is on one table and the other part is on the other one. I don't want duplicate answers.
For example say I only want items that have a type_id of 3 and a length of 18.
When I use
SELECT * FROM master_list LEFT JOIN types_join ON master_list.master_id=types_join.master_id WHERE types_join.type_id = 3 AND master_list.length = 18"
it finds the same thing twice.
How can I query this so I won't get duplicate answers?
Here are the samples from my tables and the result I am getting.
This is what I get with an INNER JOIN:
BTW, master_id and name both only have unique information on the master_list table. However, the types_join table does use the master_id multiple times later on, but not for Lye. That is why I know it is duplicating information.
If you want unique rows from master_list, use exists:
SELECT ml.*
FROM master_list ml
WHERE ml.length = 18 AND
EXISTS (SELECT 1
FROM types_join tj
WHERE ml.master_id = tj.master_id AND tj.type_id = 3
);
Any duplicates you get will be duplicates in master_list. If you want to remove them, you need to provide more information -- I would recommend a new question.
Thank you for the data. But as you can see enter link description here, there is nothing wrong with your query.
Have you tried create an unique index over master_id, just to make sure that you do not have duplicated rows?
CREATE UNIQUE INDEX MyMasterUnique
ON master_list(master_id);

Get column content from one table and use it to get id from other table

Sorry for the bad description, not sure what to call this.
I'm trying to migrate a bad database structure to a better one.
The old storage, lets say customer name in say invoice table.
The new I going to need a ref to customer-table so that more information about the customer can be queried if needed.
So, to not completely break all the old entries, I would like a query that take col2,col3,col4 from tbl1 and and queries tbl2,tbl3 and tbl4 to get and ID of
each of the content of the columns.
Then I would want to update the row in tbl1 with these ID's in their respectable column.
The thing is, I have no idea about how to do this.
I don't even know what to search for to learn about it.
Better example:
Imagen you have pet-owner table. The old table had:
Owner: John
pet: Dog
But now I have a table for owners and a table for pets, so I want to update the columns in pet-owner table so for each row it will (in this case) search ower-table for 'John' and pet-table for 'dog' and return their ID and update the pet-owner table with that.
You'll have a much easier time if you add new columns to hold the ids, populate them, and then drop the old columns later.
If you do that, your update would be like:
update pet_owner
inner join pet on pet_owner.pet_name = pet.name
inner join owner on pet_owner.owner_name = owner.name
set pet_owner.pet_id = pet.id
pet_owner.owner_id = owner.id
;

mysql query using column values as parameter in query phpMyAdmin

I have a query i have been working on trying to get a specific set of data, join the comments in duplicate phone numbers of said data, then join separate tables based on a common field "entry_id" which also happens to be the number on the end of the word custom_ to pull up that table.
table named list and tables containing the values i want to join is custom_entry_id (with entry_id being a field in list in which i need the values of each record to replace the words in order to pull up that specific table) i need entry_id from the beginning part of my query to stick onto the end of the word custom for every value my search returns to get the fields from that custom table designated for that record. so it will have to do some sort of loop i guess? sorry like i said I am at a loss at this point
this is where i am so far:
SELECT * ,
group_concat(comments SEPARATOR '\r\n\r\n') AS comments_combined
FROM list WHERE `status` IN ("SALEA","SALE")
GROUP BY phone_number
//entry_id is included in the * as well as status
// group concat combines the comments if numbers are same
i have also experimented on test data with doing a full outer join which doesnt really exist. i feel if you can solve the other part for me i can do the joining of the data with a query similar to this.
SELECT * FROM test
LEFT JOIN custom_sally ON test.num = custom_sally.num
UNION
SELECT * FROM test
RIGHT JOIN custom_sally ON test.num = custom_sally.num
i would like all of this to appear with every field from my list table in addition to all the fields in the custom_'entry_id' tables for each specific record. I am ok with values being null for records that have different custom fields. so if record 1 has custom fields after the join of hats and trousers and record 2 has socks and shoes i realize that socks and shoes for record 1 will be null and hats and trousers for record 2 will be null.
i am doing all this in phpmyadmin under the SQL tab.
if that is a mistake please advise as well. i am using it because ive only been working with SQl for a few months. from what i read its the rookie tool.
i might be going about this all wrong if so please advise
an example
i query list with my query i get 20,000 rows with columns like status, phone_number, comments, entry_id, name, address, so on.
now i want to join this query with custom fields in another table.
the problem is the custom tables' names are all linked to the entry_id.
so if entry_id is 777 then the custom table fields are custom_777
my database has over 100 custom tables with specials fields for each record depending on its entry_id.
when i query the records I don't know how to join the custom fields that are entry_id specific to the rest of my data.i will pull up some tables and data for a better example
this is the list table:
this is the custom_"entry_id"
Full Outer Join in MySQL
for info on full outer joins.

Return records from one table where Field not found in another table

I'm so lost here I don't even know how to best title my question.
I am creating a simple dating site. I want the women to be able to block the men just like all other dating sites. When this happens, I don't want the womens' profiles to be returned in a query.
Table A
Members table containing all the profile information including a member name
Table B
Blocked members table containing the woman's name and the man's name for each case in which the woman has blocked a man
So, I want something like this:
$query = Return all records from table A where sex=female and there is no record in table B containing the woman's name and the man's name
I thought I would run a query against table B to retrieve all women who have blocked me, then run a query against table A to return all females in which the woman's username is NOT contained in the results of my first query. However, I can't figure out how to do this.
If I understand your question...seems like a simple join, no? Not sure if I'm misunderstanding. Something like this perhaps:
SELECT * FROM Table1 WHERE Table1.ID NOT IN (SELECT BLOCK_ID FROM table2)
So Table1 has all ID's of the women, and Table2 has all block id's (for example) and you want what is not in that? Obviously some changes required on top of this.
If you wanted to see a list of all the female members who had blocked the current user, you would use a query like:
SELECT member.*
FROM TableA member
JOIN TableB blocked ON (member.name = blocked.user_who_blocked)
WHERE member.sex = female
AND blocked.blocked_user_name = 'Joe McCurrentUser'
;
So, if you want to see the set of users where that is not the case, you use a LEFT JOIN and look for a null id.
SELECT member.*
FROM TableA member
LEFT JOIN TableB blocked ON (member.name = blocked.user_who_blocked)
WHERE member.sex = female
AND blocked.blocked_user_name = 'Joe McCurrentUser'
AND blocked.id IS NULL
;
You can modify as you wish to use the actual columns in your tables. Make sure you have indices on both the user_who_blocked and blocked_user_name columns in that table.
Would this work?
Select * from Table A
inner join Table B on a.womans_name = B.womans_name and B.mans_name="Mans Name"
where B.womans_name IS NULL
If Table B contains a record with the matching womans_name and mans_name then the join will create one record containing all the fields in Table A and Table B but the Where clause will reject this record because the womans_name from Table B will not be null. If Table B does not contain a matching record then all those fields will be null (including B.womans_name) so the Where clause is satisfied.