Insert into table if second table has conditional value - mysql

I have two tables:
Table 1 (customers)
customer_id customer_name salesPerson_id
1 John 1
2 Ed 1
3 Sam 2
Table 2 (customerContacts)
contact_id customer_id phone_number
1 1 687-5309
2 1 555-1234
3 1 742-1111
I am trying to let only the sales person add / update a phone number for their specific customer.
So only sales salesPerson_id 1 could update John and Ed and only salesPerson_id 2 could update Sam.
I believe I am looking for something like:
INSERT INTO customerContacts (contact_id , customer_id , phone_number) VALUES (1 , 1 , '987-6543')
ON DUPLICATE KEY UPDATE phone_number='987-6543'
IF customers.salesPerson_ID = 1
But it doesn't seem like sql supports if statements.

INSERT INTO customerContacts (contact_id , customer_id , phone_number)
Select 1 , customer_id , '987-6543'
from customers
where salesPerson_ID = 1 and customer_id=1;
This is the query which you should use in the native way but you need to fit this in your application framework

Related

Temporary Table with Person Names Replicated with 12 Times, with Each Row Inserted with a Month Number

I would like to create a temporary table that extracts the first name and last name of all profiles from the [Person] table in my SQL database. Then, I want to replicate each person 12 times in this new table and add a column called Month so that each of the 12 replicated rows is inserted with a digit (1-12) to represent a month of the year. Can you tell me how to create a SQL for this?
Using MySQL UNION ALL to create a list of each month repeated for each name.
Repeat the select query once for each month, change the month number for each one:
SELECT name, 1 `month` FROM EMPLOYEE
UNION ALL
SELECT name, 2 FROM EMPLOYEE
UNION ALL
SELECT name, 3 FROM EMPLOYEE
ORDER BY month, name
;
Given EMPLOYEE table:
id
name
1
Clark
2
Dave
3
Ava
This outputs:
name
month
Ava
1
Clark
1
Dave
1
Ava
2
Clark
2
Dave
2
Ava
3
Clark
3
Dave
3
-- create
CREATE TABLE EMPLOYEE (
empId INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept TEXT NOT NULL
);
-- insert
INSERT INTO EMPLOYEE VALUES (1, 'Clark', 'Sales');
INSERT INTO EMPLOYEE VALUES (2, 'Dave', 'Accounting');
INSERT INTO EMPLOYEE VALUES (3, 'Ava', 'Sales');
INSERT INTO EMPLOYEE VALUES (4, 'Clark', 'Maintenance');
-- fetch
SELECT name, 1 `month` FROM EMPLOYEE
UNION ALL
SELECT name, 2 FROM EMPLOYEE
UNION ALL
SELECT name, 3 FROM EMPLOYEE
ORDER BY month, name
;
Try it here: https://onecompiler.com/mysql/3ygcscr33
Note: UNION ALL is required in case names are the same for multiple people.

Return count of how many occurrences there are

I have a MySQL table that looks like this:
user_id
other_id
date
123456789
123
date1
213454678
123
date2
Here, user_id is a multikey and is re-occurring for some entries. Date is irrelevant for this task.
I tried this query:
select user_id, count(user_id) as count from exp_pixel_data group by user_id;
And this returned
user_id
count
123324345456456576587
7
453545435343455343453
3
777676766776675654454
2
345565664545665654645
1
This result tells me how often a user_id is occurring in the table. This may be a good start, but now i need how often this counts are occurring in the result of the last query. So the question is, how many user_ids occur 7 times in the table?
I need a SQL query which returns something like this:
count
times_ocurring
1
123
2
100
3
2
and so on.
This means that 123 times there are user_ids that occur one time in the main table, 100 times user_ids that occur 2 Times and 2 times user_ids that occur 3 times.
Is it possible you're trying to do this (count the counts)?
Fiddle
SELECT COUNT(xcount) AS count_of_counts
, xcount
FROM (SELECT user_id
, COUNT(user_id) AS xcount
FROM exp_pixel_data
GROUP BY user_id
) xxx
GROUP BY xcount
ORDER BY COUNT(xcount)
;
and with MySQL, we can use the derived column name in the ORDER BY:
SELECT COUNT(xcount) AS count_of_counts
, xcount
FROM (SELECT user_id
, COUNT(user_id) AS xcount
FROM exp_pixel_data
GROUP BY user_id
) xxx
GROUP BY xcount
ORDER BY count_of_counts
;
Result (given test data below):
+-----------------+--------+
| count_of_counts | xcount |
+-----------------+--------+
| 1 | 5 |
| 2 | 2 |
| 2 | 1 |
+-----------------+--------+
Setup:
CREATE TABLE exp_pixel_data (user_id int, val int default 0);
INSERT INTO exp_pixel_data (user_id) VALUES
(12345)
, (12345)
, (12399)
, (12399)
, (12388)
, (12377)
, (12355)
, (12355)
, (12355)
, (12355)
, (12355)
;

Given 3 columns (sid, date, tid) and primary key (date, tid). Find the sids that have every tid

Given this relation call it
winners
sid date tid
1 2011 1
2 2012 1
1 2011 2
2 2012 2
3 2013 1
This is the table properties
CREATE TABLE winners(
sid INTEGER REFERENCES student(sid),
date INTEGER NOT NULL,
tid INTEGER REFERENCES tournament(tid),
PRIMARY KEY(tid, date));
I want to query such that I find the sids that won every tournament.
In this case I want the output to be
sid
1
2
Because there is only 2 tournaments (tid 1 and 2) and sid 1 and 2 won both of them.
MY ATTEMPT
this query will return
CREATE VIEW uniquetids AS
SELECT DISTINCT count(tid), sid FROM winners GROUP BY sid;
-----------
count sid
2 1
2 2
1 3
Now what? Not sure how to get 1 2 only
It should serve your purpose.
WITH cdata AS (
SELECT
sid,
count(DISTINCT tid) tid_count
FROM winners
GROUP BY sid
)
SELECT
sid
FROM cdata
WHERE
tid_count = (SELECT max(tid_count) FROM cdata)
Might be a little hack-ish, and would fail if you could have a student win a tournament twice. And there may be other scenarios... But with what you have:
SELECT sid
FROM (
SELECT sid, count(1) won
FROM winners
GROUP BY sid
) win_counts
WHERE won = (SELECT count(tid) FROM TOURNAMENT)

Duplicate all rows and change but change store_id

I have this table name: copy_stores
copy_id | store_id
11221 2
11222 2
112223 2
there is about 2000 records, but I like to duplicate all the records but on the newly duplicated change the store_id to 1 where it's 2
I have tried this, but it won't work:
insert into copy_stores(`copy_id`, `store_id`)
SELECT 1, `copy_id`, `store_id`
from copy_stores
where store_id = 2
You want '1' to be the new store_id, so need to select it after the copy_id:
INSERT INTO copy_stores(copy_id, store_id)
SELECT copy_id, 1
FROM review_store
WHERE store_id = 2

How to get ID for INSERT if name already used, else generate new one

I'm having a bit of trouble with an INSERT query.
I have a table I'm inserting a value into that's like this:
TABLE cars
ID Brand Model B_ID
---------------------------
1 Ford Escort 1
2 Ford Focus 1
3 Nissan Micra 2
4 Renault Megane 3
5 Ford Mustang 1
ID is unique and B_ID is the same ID for every same brand.
When inserting a new entry I want to be able to check if a brand is already in there and use that same B_ID otherwise I want to increment the highest B_ID and insert that.
I've got this far:
INSERT INTO 'cars' ('brand', 'model', 'B_ID')
VALUES (
'Nissan'
'Note'
'SELECT B_ID FROM cars WHERE brand = 'Nissan'
)
How can I get the highest B_ID and increment it by one if there is no match with my subquery because it's a new brand?
I'm using MySQL.
INSERT INTO `cars` (`brand`, `model`, `B_ID`)
select 'Nissan', 'Note', coalesce(Max(B_ID),0)+1 FROM cars WHERE brand = 'Nissan'
Until you normalize your tables:
INSERT INTO cars
(brand, model, B_ID)
SELECT 'Nissan'
, 'Note'
, COALESCE( ( SELECT B_ID
FROM cars
WHERE brand = 'Nissan'
LIMIT 1
)
, ( SELECT MAX(B_ID)
FROM cars
) + 1
, 1 --- this is for the case when the table is empty
)
Also notice that if you have multiple concurrent INSERT, you may end with rows that have different brand but same B_ID.