Trying to update table in mysql 8 gives error - mysql

I am trying to update a table that I am working with.
Here is the "describe table;" output:
MySQL [mydb]> describe ost_staff;
+------------------------+-------------------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-------------------------------------------+------+-----+---------+----------------+
| staff_id | int unsigned | NO | PRI | NULL | auto_increment |
| dept_id | int unsigned | NO | MUL | 0 | |
| role_id | int unsigned | NO | | 0 | |
| username | varchar(32) | NO | UNI | | |
| firstname | varchar(32) | YES | | NULL | |
| lastname | varchar(32) | YES | | NULL | |
| passwd | varchar(128) | YES | | NULL | |
| backend | varchar(32) | YES | | NULL | |
| email | varchar(255) | YES | | NULL | |
| phone | varchar(24) | NO | | | |
| phone_ext | varchar(6) | YES | | NULL | |
| mobile | varchar(24) | NO | | | |
| signature | text | NO | | NULL | |
| lang | varchar(16) | YES | | NULL | |
| timezone | varchar(64) | YES | | NULL | |
| locale | varchar(16) | YES | | NULL | |
| notes | text | YES | | NULL | |
***| isactive | tinyint(1) | NO | MUL | 1 | |***
I am trying to update the field: isactive so I try the following command from one of the solutions that I found online:
UPDATE `ost_staff` SET `value`=0 WHERE `key`='isactive';
when I ran this command, I get the following error:
ERROR 1054 (42S22): Unknown column 'key' in 'where clause'
I am trying to change the value from 1 to zero in "Default" field.

The error message is self explanary...
There is no column key in your table.
The request should be something like
UPDATE `ost_staff` SET `isactive`=0 WHERE `staff_id`=XX;
or
UPDATE `ost_staff` SET `isactive`=0
to set 0 to all staff.

Related

MySQL: Trying to return NULL values in a ER model, but not having any luck

I have created a realistic table set using the ER model for MySQL. I am attempting to do joins, but seem to either be missing something, or not doing them correctly in order to produce NULL values. Below are my created tables:
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| FilmNumber | char(3) | NO | PRI | NULL | |
| FilmName | varchar(60) | YES | | NULL | |
| OpeningDate | varchar(20) | YES | | NULL | |
| TopBilledActor | char(60) | YES | | NULL | |
| Genre | varchar(20) | YES | | NULL | |
+----------------+-------------+------+-----+---------+-------+
+------------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+-------------+------+-----+---------+-------+
| CriticSiteNumber | char(3) | NO | PRI | NULL | |
| CriticSiteName | varchar(30) | YES | | NULL | |
| CriticName | char(50) | YES | | NULL | |
+------------------+-------------+------+-----+---------+-------+
+------------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+---------+------+-----+---------+-------+
| FilmNumber | char(3) | YES | MUL | NULL | |
| CriticSiteNumber | char(3) | YES | MUL | NULL | |
| OverallScore | int(5) | YES | | NULL | |
+------------------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)
My question is, what type of join query would I need in order to produce NULL values, or does something in my table need to be modified because each of the queries I attempt do not return those values.

why is MySQL casting varchar as integer when WHERE clause is present?

I have a MySQL database table, student_import_record, with a student_id varchar(50) field that can contain alphanumeric data.
When I run this SELECT statement:
select student_id from student_import_record;
I get the VARCHAR results as expected:
0001546
0001660
0002207
0002349
But when I run this SELECT statement with a WHERE clause:
select student_id from student_import_record where student_import_id = 185;
I get results that appear to be cast as an integer:
1546
1660
2207
2349
How do I prevent this casting when using a WHERE clause?
Full Table schema:
mysql> describe student_import_record;
+--------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| student_id | varchar(50) | NO | | NULL | |
| relationship | varchar(50) | NO | | NULL | |
| date_of_birth | varchar(50) | NO | | NULL | |
| first_name | varchar(255) | NO | | NULL | |
| last_name | varchar(255) | NO | | NULL | |
| gender | varchar(10) | NO | | NULL | |
| student_type | varchar(50) | YES | | NULL | |
| email | varchar(255) | YES | | NULL | |
| phone | varchar(20) | YES | | NULL | |
| address | varchar(255) | YES | | NULL | |
| address2 | varchar(255) | YES | | NULL | |
| city | varchar(255) | YES | | NULL | |
| state | varchar(50) | YES | | NULL | |
| zip | varchar(50) | YES | | NULL | |
| mandate | varchar(10) | YES | | NULL | |
| ssn | varchar(11) | YES | | NULL | |
| suffix | varchar(20) | YES | | NULL | |
| department | varchar(50) | YES | | NULL | |
| password | varchar(255) | YES | | NULL | |
| benefit1 | varchar(255) | YES | | NULL | |
| benefit2 | varchar(255) | YES | | NULL | |
| benefit3 | varchar(255) | YES | | NULL | |
| benefit4 | varchar(255) | YES | | NULL | |
| benefit5 | varchar(255) | YES | | NULL | |
| benefit6 | varchar(255) | YES | | NULL | |
| plan1 | varchar(255) | YES | | NULL | |
| plan2 | varchar(255) | YES | | NULL | |
| plan3 | varchar(255) | YES | | NULL | |
| plan4 | varchar(255) | YES | | NULL | |
| plan5 | varchar(255) | YES | | NULL | |
| plan6 | varchar(255) | YES | | NULL | |
| account | int(11) | NO | | NULL | |
| enrollment_config | int(11) | NO | | NULL | |
| imported_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
| status | smallint(6) | YES | | NULL | |
| status_description | longblob | YES | | NULL | |
| student_import_id | int(11) | NO | | NULL | |
Result sets
mysql> select student_id from student_import_record where student_import_id = 185 limit 10;
+------------+
| student_id |
+------------+
| 1546 |
| 1660 |
| 2207 |
| 2349 |
| 3123 |
| 3208 |
| 3319 |
| 3811 |
| 3837 |
| 3842 |
+------------+
10 rows in set (0.02 sec)
mysql> select student_id from student_import_record limit 10;
+------------+
| student_id |
+------------+
| 0001546 |
| 0001660 |
| 0002207 |
| 0002349 |
| 0003123 |
| 0003208 |
| 0003319 |
| 0003811 |
| 0003837 |
| 0003842 |
+------------+
10 rows in set (0.00 sec)
Looks like you have already figured out what was going on. In the hope to close out this question, here's a link that explains type conversion in expression evaluation: https://dev.mysql.com/doc/refman/5.5/en/type-conversion.html
When an operator is used with operands of different types, type
conversion occurs to make the operands compatible. Some conversions
occur implicitly. For example, MySQL automatically converts numbers to
strings as necessary, and vice versa.
mysql> SELECT 1+'1';
-> 2
In your case, MySQL might be seeing arithmetic operation in the WHERE clause and performing implicit conversion to integers in that column. My recommendation is to structure the table, if possible, in a manner that numbers are in numeric column and non-numeric data is in its appropriate column. When there's mixed content, you could force the data to be character.

MySQL command run on terminal is stuck, but phpmyadmin success

I want to get location from members IP. I can use the command on phpmyadmin, and it costs about 2 seconds. But it will be stuck when the command is executed on MySQL through terminal. How can I fix the problem.
SQL Command:
SELECT
ID,country
FROM
(SELECT ID,substring_index(members.server_ids,',',1) AS IP FROM members) AS A
JOIN
(SELECT country,start,end FROM ip) AS B
ON
INET_ATON(A.IP) BETWEEN INET_ATON(B.start) AND INET_ATON(B.end);
desc members:
+---------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+--------------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| USER_NAME | varchar(100) | NO | UNI | NULL | |
| PASSWORD | varchar(32) | YES | | NULL | |
| NAME | varchar(100) | YES | | NULL | |
| EMAIL | varchar(50) | YES | MUL | NULL | |
| SAFE_EMAIL | varchar(50) | YES | | NULL | |
| NICK_NAME | varchar(100) | YES | MUL | NULL | |
| GENDER | tinyint(1) | YES | | 0 | |
| BIRTHDAY | date | YES | | NULL | |
| CREATEDAY | datetime | YES | | NULL | |
| PHONE | varchar(20) | YES | | NULL | |
| MOBILE | varchar(15) | YES | | NULL | |
| ID_CARD | varchar(32) | YES | | NULL | |
| COUNTY | int(11) | YES | | NULL | |
| ADDRESS | varchar(255) | YES | | NULL | |
| LOGIN_TIME | datetime | YES | | NULL | |
| BUY_SN | varchar(50) | YES | | NULL | |
| BUY_KIND | varchar(50) | YES | | NULL | |
| PARTNER_SN | varchar(50) | YES | | NULL | |
| cookie_value | varchar(100) | YES | | NULL | |
| CONSIGNEE_INFO | int(11) | YES | MUL | NULL | |
| LOGIN_COUNT | int(11) | YES | | NULL | |
| PAY_COUNT | int(11) | YES | | NULL | |
| status | tinyint(4) | YES | | 1 | |
| experience | int(11) | YES | | 0 | |
| amount | int(11) | YES | | 0 | |
| score | int(11) | YES | | 0 | |
| is_facebook_account | tinyint(4) | YES | | 0 | |
| avatar | varchar(200) | YES | | NULL | |
| avatar_status | tinyint(1) | YES | | 0 | |
| member_authhash | varchar(50) | YES | | NULL | |
| job | varchar(50) | YES | | NULL | |
| server_ids | varchar(255) | YES | | NULL | |
+---------------------+--------------+------+-----+---------+----------------+
SELECT ID,server_ids FROM members LIMIT 10000,1;
+-------+--------------------------+
| ID | server_ids |
+-------+--------------------------+
| 20005 | 61.20.167.219, 127.0.0.1 |
+-------+--------------------------+
desc ip:
+---------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| country | char(2) | NO | | NULL | |
| type | char(4) | NO | | NULL | |
| start | varchar(15) | NO | MUL | NULL | |
| end | varchar(15) | NO | MUL | NULL | |
+---------+------------------+------+-----+---------+----------------+
SELECT * FROM ip LIMIT 100,1;
+-----+---------+------+-----------+---------------+
| id | country | type | start | end |
+-----+---------+------+-----------+---------------+
| 101 | IN | ipv4 | 1.187.0.0 | 1.187.255.255 |
+-----+---------+------+-----------+---------------+
phpMyAdmin automatically adds a LIMIT clause to the query. Because you do not have an ODER BY clause, MySQL sends the results as soon as it has found the number of records that you requested in the LIMIT clause.
Add a LIMIT clause to the query that you execute on the command line.

how to add a foreign key using an existing column in mysql

I have following table called transactions and I want to add foreign key using column account_id. But when I execute the query (see below) 0 rows affected. I am not getting what is wrong with the query.
I have two tables called transactions and accounts. Accounts table has an id as primary key and an account has_many transactions.
transactions table
+---------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| account_number | int(11) | YES | | NULL | |
| m_number | varchar(255) | YES | | NULL | |
| registration_number | varchar(255) | YES | | NULL | |
| page_number | varchar(255) | YES | | NULL | |
| entry_date | datetime | YES | | NULL | |
| narration | varchar(255) | YES | | NULL | |
| voucher_number | varchar(255) | YES | | NULL | |
| debit_credit | float | YES | | NULL | |
| profit | float | YES | | NULL | |
| account_code | int(11) | YES | | NULL | |
| balance | float | YES | | NULL | |
| branch | varchar(255) | YES | | NULL | |
| account_id | int(11) | YES | MUL | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+---------------------+--------------+------+-----+---------+----------------+
16 rows in set (0,01 sec)
mysql> ALTER TABLE transactions ADD FOREIGN KEY (account_id) REFERENCES accounts(id);
Query OK, 0 rows affected (0,03 sec)
Records: 0 Duplicates: 0 Warnings: 0
accounts table
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| account_number | int(11) | YES | | NULL | |
| account_code | int(11) | YES | | NULL | |
| branch | varchar(255) | YES | | NULL | |
| name | varchar(255) | YES | | NULL | |
| father_name | varchar(255) | YES | | NULL | |
| nic_number | varchar(255) | YES | | NULL | |
| mohalla | varchar(255) | YES | | NULL | |
| village | varchar(255) | YES | | NULL | |
| nominee | varchar(255) | YES | | NULL | |
| relationship | varchar(255) | YES | | NULL | |
| opening_balance | float | YES | | NULL | |
| opening_date | datetime | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
After running the query shouldn't the column say F_K or something like PRI ?
Any help would be great. Thanks!

Include null values in SQL query

I have the following four tables in my database:
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| complex_id | int(11) | NO | PRI | NULL | auto_increment |
| complex_name | varchar(45) | NO | | NULL | |
+--------------+-------------+------+-----+---------+----------------+
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| machine_id | int(11) | NO | PRI | NULL | auto_increment |
| complex_id | int(11) | NO | MUL | NULL | |
| machine_name | varchar(45) | NO | | NULL | |
+--------------+-------------+------+-----+---------+----------------+
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| devices_id | int(11) | NO | PRI | NULL | auto_increment |
| machine_id | int(11) | NO | MUL | NULL | |
| description | varchar(255) | NO | | NULL | |
| location | varchar(255) | YES | | NULL | |
| verification | varchar(255) | YES | | NULL | |
| rack_num | varchar(8) | YES | | NULL | |
| section_num | varchar(8) | YES | | NULL | |
| color_or_number | varchar(16) | YES | | NULL | |
| normal_position | varchar(16) | YES | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| pnp_id | int(11) | NO | PRI | NULL | auto_increment |
| devices_id | int(11) | NO | MUL | NULL | |
| pnp_num | varchar(45) | NO | | NULL | |
+------------+-------------+------+-----+---------+----------------+
I am trying to get results formatted as follows (NULL values appear as blanks):
+------------+-------------+-----------------------+
| devices_id | description | pnpnum |
+------------+-------------+-----------------------+
| 1 | ex | 1234 |
| 2 | ex2 | 2345 |
| 3 | ex3 | |
| 4 | ex4 | 3456, 4567, 5678, 6879|
+------------+-------------+-----------------------+
Using the following SQL query,
SELECT *, GROUP_CONCAT(pnp.pnp_num separator ', ') pnpnum
FROM devices
JOIN pnp ON devices.devices_id = pnp.devices_id
WHERE devices.machine_id = 1
GROUP BY devices.devices_ID
ORDER BY devices.description;
my results are relatively close, however, I am unable to include a device if it has a null pnpnum.
+------------+-------------+-----------------------+
| devices_id | description | pnpnum |
+------------+-------------+-----------------------+
| 1 | ex | 1234 |
| 2 | ex2 | 2345 |
| 4 | ex4 | 3456, 4567, 5678, 6879|
+------------+-------------+-----------------------+
What is it that I am missing from my SQL statement that will allow me to include null values?
You need to use LEFT JOIN because even if there isn't a match, it will return all the results from the left table leaving the fields from the right table null.