Showing posts with label mysql. Show all posts
Showing posts with label mysql. Show all posts

February 23, 2021

MySQL: SELECT an order having 2 or more identical products

SELECT 
    op.order_id, op.product_id, COUNT(op.product_id)
FROM
    order_product op
GROUP BY op.order_id, op.product_id
HAVING COUNT(op.product_id) >= 2
with a join to the actual orders table
SELECT 
    o.order_id, op.product_id, COUNT(op.product_id)
FROM
    `order` o
        INNER JOIN
    order_product op ON (o.order_id = op.order_id)
WHERE
    o.store_id = 18
        AND o.date_added > '2020-01-01 00:00:00'
        AND o.date_added < '2020-12-29 12:08:00'
GROUP BY o.order_id , op.product_id
HAVING COUNT(op.product_id) >= 2
ORDER BY o.date_added DESC

May 31, 2019

mysql rsnapshot backup separate database table files

Backing up MySQL databases and tables as separate files with rsnapshot. Simply copying /var/lib/mysql won't always work, so the databases need to be exported.

First, create a separate MySQL user backup with these limited to read-only permissions: LOCK TABLES, SELECT, SHOW VIEW.

Install rsync - otherwise you'd get bash: rsync: command not found; rsync: connection unexpectedly closed.

Then set up this script:

#! /bin/bash

BACKUP_DIR="/var/backups/mysql"
MYSQL_USER="backup"
MYSQL_PASSWORD="hackme"
MYSQL=$(which mysql)
MYSQLDUMP=$(which mysqldump)

databases=`$MYSQL --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema)"`

for db in $databases; do
  for tbl in `$MYSQL -u $MYSQL_USER -p$MYSQL_PASSWORD -N -B -e "SHOW TABLES FROM $db"`; do
    VAR_LOCATION="${BACKUP_DIR}/${db}/${tbl}.sql"
    mkdir -p "$(dirname ${VAR_LOCATION})"
    $MYSQLDUMP --skip-comments --compact -u $MYSQL_USER -p$MYSQL_PASSWORD $db $tbl > "${VAR_LOCATION}"
  done
done

The above is a combination of these scripts from mensfeld.pl and jamescoyle.net.

I previously tried using the following line from this answer:

mysqldump --user=dbuser --password --tab=~/output/dir dbname
But that doesn't work, really: it throws Access denied (using password: YES) when executing 'SELECT INTO OUTFILE' because it requires a special kind of GRANT you can't easily set via the MySQL Workbench UI. After all, you end up with a CSV backup which you have to treat differently.