MySQL if value exists select else insert [closed] - mysql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
What is the most efficient mysql query to SELECT from a table IF a value already exists ELSE INSERT?
I have already tried several options but can't find something that works for me.

I'm new to Stackoverflow and dont know how to tag a question as duplicate.
But i think there is a really similar question with plenty of answers. Give a look here
[Possible solution]
You could insert if it not exists and then select it.
INSERT INTO Test(Key_Value , Name , ...) VALUES(#Key_Value ,#Name,..) WHERE NOT EXISTS (SELECT 1 FROM Test WHERE KeyValue = #KeyValue);
SELECT Name FROM Test WHERE Key_Value = #Key_Value

Related

MySql : How to delete multiple rows? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
How should I delete those two rows by using user_email and note_name only?
Please help
thank you
You can use this below delete script-
delete from your_table_name where user_email = 'asdf#....' and note_name = 'adsf'
Note: This will also delete other rows if there any matching for the conditions.

How to get a specific row of data for mysql with specification of company and agent [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How to retrieve all the data pertaining to company name and agent from mysql? How to write the query statement?
Have you tried this?
SELECT *
FROM agent_info
WHERE policyCompany = 'Avia' and policyAgent = 'Ron';
I have to guess because you didn't tell us much about your tables, but this might work.
SELECT policyCompany,
GROUP_CONCAT(DISTINCT policyAgent ORDER BY policyAgent) agents
FROM mytable
GROUP BY policyCompany
You can read about GROUP_CONCAT() here.

MYSQL - Displaying 2 types of data based on 2 different columns [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
List the Book Code and Book Title of each book with the Type SFI and is in paperback.
So I'm trying to run a query for one of the labs at university and I'm not sure where I'm going wrong.
Here's what I have so far
SELECT BOOK_CODE, TITLE
FROM BOOK
WHERE (PAPERBACK = 'Y', TYPE = 'SFI')
Your where condition is not correct
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
You can find some examples here or in your professors slides... ;)

sql query to insert multiple records [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I've a table "user" and another table "test"
My test table contains some of the userids. I want to check and insert into table "test" all those userids which are not there.
can you please help me with SQL query ?
thanks in advance
You may looking for this
INSERT INTO Test(Col1,col2..)
SELECT Col1,col2.. FROM UserTable WHERE UserTable.UserID NOT IN (SELECT UserID FROM Test)

INSERT...SELECT FROM Mysql statement error [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a list of items in a table that have ids, and I want to put them into a item_to_group table, all with a specific group number.
My MySQL code is:
INSERT INTO `item_to_item_group` (`items`.`id`, 3476)
SELECT `id`
FROM `items`
But I'm getting the old "You have an error in your SQL syntax" message. Can anyone help? Thanks
No, it should be the column name instead of value,
INSERT INTO item_to_item_group (id, columnameHere) -- <== define columnName here
SELECT id, 3476
FROM items