martes, 26 de octubre de 2021

MYSQL display duplicated values

SELECT      list_phone,list_name,COUNT(list_phone) FROM     lists where list_name='4thbatch-phoneonly' GROUP BY list_phone HAVING COUNT(list_phone) > 1;



| 9896652563 | 4thbatch-phoneonly |                 3 |

| 9896895556 | 4thbatch-phoneonly |                 3 |

| 9897435168 | 4thbatch-phoneonly |                 3 |

| 9897529384 | 4thbatch-phoneonly |                 2 |

| 9898399391 | 4thbatch-phoneonly |                 4 |

| 9898457427 | 4thbatch-phoneonly |                 2 |

| 9898766241 | 4thbatch-phoneonly |                 2 |

| 9898927972 | 4thbatch-phoneonly |                 3 |

+------------+--------------------+-------------------+

1943 rows in set (0.10 sec)


https://www.mysqltutorial.org/mysql-find-duplicate-values/


martes, 31 de agosto de 2021

subqueries

 SELECT (SELECT COUNT(*) FROM campaigns) as Campaigns, (SELECT COUNT(DISTINCT(list_name)) FROM lists where list_name!='TESTLOCAL' ) as list




You can do it by using subqueries, one subquery for each tableCount :

SELECT
  (SELECT COUNT(*) FROM table1 WHERE someCondition) as table1Count, 
  (SELECT COUNT(*) FROM table2 WHERE someCondition) as table2Count,
  (SELECT COUNT(*) FROM table3 WHERE someCondition) as table3Count

https://stackoverflow.com/questions/3761240/count-from-multiple-tables-in-mysql

jueves, 26 de agosto de 2021

create user and assign select permission to specific DB

 CREATE USER 'webdev'@'localhost' IDENTIFIED BY 'M1212';


GRANT SELECT ON dialer.* TO webdev@localhost IDENTIFIED BY 'M1212';


https://stackoverflow.com/questions/20036547/mysql-grant-read-only-options

viernes, 22 de enero de 2021

delete records on 2 table

delete all tables data

 DELETE cdr,campaigns FROM cdr INNER JOIN campaigns ON campaigns.cp_name = cdr.call_cp WHERE cdr.call_cp =campaigns.cp_name


delete all records from table cdr and campaigns where where campaigns.cp_name and  cdr.call_cp is equal A

DELETE cdr,campaigns FROM cdr INNER JOIN campaigns ON campaigns.cp_name = cdr.call_cp WHERE cdr.call_cp ='A';


MYSQL tutorial

 MYSQL tutorial


https://www.mysqltutorial.org/

domingo, 7 de octubre de 2018

How to Transfer All MySQL Databases From Old to New Server


lunes, 5 de marzo de 2018

Finding the Second Highest Salary:


Consider the following table. It consists of a set of employee records and we are going to use in our queries to get salary that is second largest.

Table Name: Employees

IDNameDesignationSalary
1Colleen HurstRegional Director205500
2Garrett WintersAccountant170750
3Quinn FlynnSupport Lead342000
4Jena GainesSoftware Engineer133600
5Brielle WilliamRegional Director372000

1. Using Subquery:

Here is the simple query to find the highest salary from the above 'Employees' table. It uses the max() aggregate function to return the maximum value of a field (or expression).
SELECT MAX(Salary) FROM Employees

// output: 372000
Simply nesting this query will give you the second highest salary. It excludes the maximum salary from the table using subquery and fetches the maximum salary again.
SELECT MAX(Salary) FROM Employees WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employees)

// output: 342000
Below is the slightly different version of this sub query and it uses < (less than) operator instead of NOT IN
SELECT MAX(Salary) FROM Employees WHERE Salary < (SELECT MAX(Salary) FROM Employees)

// output: 342000

2. Using Limit Clause:

To find the second highest salary without sub-query, you can use use LIMIT and ORDER BY clause.
SELECT Salary FROM Employees ORDER BY Salary DESC LIMIT 1,1

// Output: 342000
The query will simply sort the salary field in descending order and fetch the second row (2nd largest) from the result set. If you have duplicate values in salary column, then use DISTINCT to select only unique values.
You can generalize the limit query to find out the n-th highest salary like this,
SELECT DISTINCT(Salary) FROM Employees ORDER BY Salary DESC LIMIT (n-1),1

3. With Self Join:

Another interesting way to write the query is to use self join. It takes up the cross-product of the table with itself, exclude the highest salary from the lot and then get the maximum salary from the remaining rows - which gives you the second-maximum.
Select MAX(Emp1.Salary) FROM Employees Emp1, Employees Emp2 WHERE Emp1.Salary < Emp2.Salary

// output: 342000
http://www.kodingmadesimple.com/2018/03/find-second-highest-salary-mysql.html?utm_content=bufferf1a46&utm_medium=social&utm_source=facebook.com&utm_campaign=buffer

Read Also: