$users
array according to "score"
value
$users = array(
[
'id' => 1,
'name' => 'vighac tipovichi',
'score' => 90
],
[
'id' => 2,
'name' => 'sxva tipson',
'score' => 100
],
);
__________________________________________
1st method is using this custom function:
array_sort($users, 'score', SORT_DESC)
function array_sort($array, $on, $order=SORT_ASC){
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
switch ($order) {
case SORT_ASC:
asort($sortable_array);
break;
case SORT_DESC:
arsort($sortable_array);
break;
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}
return $new_array;
}
__________________________________________
2nd method is by using usort
built-in function which allows us to use "user defined" callback/function to sort array:
1.either by declaring another function dedicated to sort array as you wish
function custom_sort_users($a, $b) {
if ($a['score'] == $b['score']) {
return 0;
}
return ($a['score'] > $b['score']) ? -1 : 1;
}
usort($users, "custom_sort_users");
2.or using callback:
usort($users, function($a, $b){
if ($a['score'] == $b['score']) {
return 0;
}
return ($a['score'] > $b['score']) ? -1 : 1;
});
SELECT
email,
COUNT(email) AS cnt
FROM
contacts
GROUP BY email
HAVING cnt > 1;
Find duplicate values in multiple columns:
SELECT
first_name, COUNT(first_name) as cnt_fn,
last_name, COUNT(last_name) as cnt_ln,
email, COUNT(email) as cnt_m
FROM
users
GROUP BY
first_name ,
last_name ,
email
HAVING cnt_fn > 1
AND cnt_ln > 1
AND cnt_m > 1;
SELECT
COUNT(*)
FROM
customers;
MySQL row count of two or more tables:
SELECT
'customers' tablename,
COUNT(*) rows
FROM
customers
UNION
SELECT
'orders' tablename,
COUNT(*) rows
FROM
orders;
personal_id
after api_method
column in requests
table ALTER TABLE requests
ADD COLUMN personal_id VARCHAR(15) AFTER api_method;
If we run the same alter
statement one more time, we will get the following error with code 1060
Error Code: 1060. Duplicate column name 'personal_id'
If we want to delete a column from a table, we use the following commandALTER TABLE `requests`
DROP COLUMN `status_id`;
SELECT * FROM Table1;
SELECT * FROM Table2;
INNER JOIN
SELECT *
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.fk = t2.id;
LEFT OUTER JOIN
SELECT *
FROM Table1 t1
LEFT OUTER JOIN Table2 t2
ON t1.fk = t2.id;
RIGHT OUTER JOIN
SELECT *
FROM Table1 t1
RIGHT OUTER JOIN Table2 t2
ON t1.fk = t2.id;
SEMI JOIN – Similar to INNER JOIN, with less duplication.
SELECT *
FROM Table1 t1
WHERE EXISTS (SELECT 1
FROM Table2 t2
WHERE t1.fk = t2.id
);
ANTI SEMI JOIN
SELECT *
FROM Table1 t1
WHERE NOT EXISTS (SELECT 1
FROM Table2 t2
WHERE t1.fk = t2.id
);
LEFT OUTER JOIN with exclusion
SELECT *
FROM Table1 t1
LEFT OUTER JOIN Table2 t2
ON t1.fk = t2.id
WHERE t2.id is null;
RIGHT OUTER JOIN with exclusion
SELECT *
FROM Table1 t1
RIGHT OUTER JOIN Table2 t2
ON t1.fk = t2.id
WHERE t1.fk is null;
FULL OUTER JOIN
SELECT * FROM Table1 t1
LEFT OUTER JOIN Table2 t2
ON t1.fk = t2.id
UNION
SELECT * FROM Table1 t1
RIGHT OUTER JOIN Table2 t2
ON t1.fk = t2.id;
FULL OUTER JOIN with exclusion
SELECT * FROM Table1 t1
LEFT OUTER JOIN Table2 t2
ON t1.fk = t2.id
WHERE t2.id IS NOT NULL
UNION
SELECT * FROM Table1 t1
RIGHT OUTER JOIN Table2 t2
ON t1.fk = t2.id
WHERE t1.ID IS NOT NULL;
Two INNER JOINs
SELECT *
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.fk = t2.id
INNER JOIN Table3 t3
ON t1.fk_table3 = t3.id;
Two LEFT OUTER JOINS
SELECT *
FROM Table1 t1
LEFT OUTER JOIN Table2 t2
ON t1.fk = t2.id
LEFT OUTER JOIN Table3 t3
ON t1.fk_table3 = t3.id;
INNER JOIN and a LEFT OUTER JOIN
SELECT *
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.fk = t2.id
LEFT OUTER JOIN Table3 t3
ON t1.fk_table3 = t3.id;
SELECT table_schema "database", sum(data_length + index_length)/1024/1024/1024 "size in GB"
FROM information_schema.TABLES GROUP BY table_schema;
The following query can show the size (MB) of all the database in a table view :
SELECT table_schema 'database', ROUND(SUM(data_length + index_length) / 1024 / 1024, 1) "DB Size in MB"
FROM information_schema.tables
GROUP BY table_schema;
The following query can show only the size of a specific database:
SELECT table_schema,
ROUND(SUM(data_length + index_length) / 1024 / 1024, 1) "DB Size in MB"
FROM information_schema.tables WHERE table_schema='DB_NAME'
GROUP BY table_schema ;
The following query can show total tables, the total table row, DB size of a specific database:
SELECT TABLE_SCHEMA AS DB_Name, count(TABLE_SCHEMA) AS Total_Tables,
SUM(TABLE_ROWS) AS Total_Tables_Row,
ROUND(sum(data_length + index_length)/1024/1024) AS "DB Size (MB)", ROUND(sum( data_free )/ 1024 / 1024) AS "Free Space (MB)"
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'DB_NAME'
GROUP BY TABLE_SCHEMA ;
<div id="some-id">Text to copy</div>
copyToClipboard('some-id');
function copyToClipboard(containerid) {
if (document.selection) {
let range = document.body.createTextRange();
range.moveToElementText(document.getElementById(containerid));
range.select().createTextRange();
document.execCommand("copy");
} else if (window.getSelection) {
let range = document.createRange();
range.selectNode(document.getElementById(containerid));
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
document.execCommand("copy");
window.getSelection().removeAllRanges();
}
}
true
if needle
is found inside haystack
inArray('find_me', ['text1', 'text2', 'find_me', 'text3']);
returns true
function inArray(needle, haystack) {
var length = haystack.length;
for(var i = 0; i < length; i++) {
if(haystack[i] === needle) { return true };
}
return false;
}