發表文章

目前顯示的是有「MySQL」標籤的文章

MySQL/MariaDB 查詢多表格並合併成單一紀錄 select as JOIN

通常查詢多表格並且同時輸出在同筆紀錄,如同JOIN in SQL,可讓PHP直接用mysql_fetch_array()抓取陣列值成為變數。可使用以下方法 例如表格 A, B 個別含以下欄位 table A   table B pid       id field1    field2 field2    field3           pid SQL: select pid, id, field1, GROUP_CONCAT(field2),field3 from A,B where A.pid=B.pid group by pid 參考 https://stackoverflow.com/questions/12890071/select-from-multiple-tables-mysql

MySQL取消索引值與改變欄位順序

1. 取消欄位索引標籤 drop index [field_name] on [table_name]; 2. 列出索引鍵 show keys from [table_name]; 3.改變欄位順序 alter table [table_name] modify [field_A] charvar(64) after [field_C]; #field_A 移到 field_C 後面 alter table [table_name] modify [field_A] charvar(64) first [field_D]; #field_A 移到 field_D 後面

MySQL8 於 CentOS 7/RHEL安裝後預設密碼

圖片
利用yum 安裝 MySQL 8 後(https://dev.mysql.com/doc/refman/8.0/en/linux-installation-yum-repo.html,https://dev.mysql.com/downloads/repo/yum/), 或 wget https://dev.mysql.com/get/mysql80-community-release-el7-2.noarch.rpm yum localinstall  mysql80-community-release-el7-2.noarch.rpm yum install mysql-community-server 安裝後 mysql 之管理帳號 root已經不再是空密碼,需先找尋預設密碼,才能繼續使用mysql_secure_installation 接著搜尋預設密碼,利用指令grep $ su - $ grep 'temporary password' /var/log/mysqld.log 若要搭配phpMyAdmin使用會發生cache_sha2_password問題 (圖一) 請記得修改如以下 msyql -u root -p >alter user 'root'@'localhost' identified with mysql_native_password by 'password'; 參考如下 https://dev.mysql.com/doc/refman/8.0/en/linux-installation-yum-repo.html https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password

MariaDB 查詢特殊條件

有些特殊條件查詢,需要利用到組合式或JOIN方式查詢。例如:我們要查詢該表格內,有符合在其他表格的條件,皆不要列出的資料。此時可以用 select * from this_table where condition not in ( select condition from another_table ); 或 select * from this_table except select condition from another_table; 參考資料 https://stackoverflow.com/questions/8768254/select-values-from-a-table-where-exclude-values-in-another-table https://mariadb.com/kb/en/library/subqueries-and-joins/

MariaDB / MySQL 手動建立資料庫與使用帳號

1. 安裝完套件後執行 mysql_secure_installation 2. 用root登入,並輸入上一步設定的密碼 mysql -u root -p 3. 新增資料庫,並設定語系編碼 create database mydb  characer set = utf8mb4  collate =  utf8mb4_general_ci ; 4. 新增使用者,並設定密碼 create user dbuser @ localhost  identified by ' mypasswd' ; 5. 新增使用者對資料庫權限 grant all privileges on mydb.* to dbuser @ localhost ; 6. 匯入您再別台機器匯出的.sql資料庫,如檔名為 export.sql mysql -u dbuser -p mydb < export.sql 接著就可以用使用者dbuser登入了 以上藍色字為隨個人喜好與需求變更。 若要改變使用密碼 可利用以下指令 mysql -u root -p 登入後 use mysql; update user set password=password('yourpass')  where user='username' and host='localhost'; flush privileges; 參考 https://www.tecmint.com/change-mysql-mariadb-root-password/