Count field other than id field [duplicate] - php

This question already has answers here:
MySQL query to count non-null values in a single row
(2 answers)
Closed 4 years ago.
I have 5 columns in my SQL database
id, name, email, phone, and country
id: total row 54
email total row 45
How I will count how many emails in my database.
I use COUNT function, but not give actual results in case of email.
select count(company.email) from company

SELECT COUNT(company.email) FROM company WHERE company.email IS NOT NULL AND company.email <> ''
This query will exclude the rows that you have no email (either NULL or just empty field)

if email column contains null,
This should work.
SELECT COUNT(!ISNULL(email)) FROM company

select count(email) from company where company<>''
NULL will get off the count by themselves

Related

Insert different content IF SQL table contains rows [duplicate]

This question already has answers here:
How to treat MAX() of an empty table as 0 instead of NULL
(3 answers)
SELECT max(x) is returning null; how can I make it return 0?
(7 answers)
Closed 1 year ago.
I am trying to insert different content based on if a SQL table has Rows. So far the Query will check what the max value of sortingOrder is and then + 1. However this query will break if there is no rows in the table. How can I implement a if statement to check if the table has no rows and then if it doesn't set the sortingOrder to '1'.
INSERT INTO faq (question, answer, sortingOrder)
VALUES ('$questionData', '$answerData', (SELECT MAX(sortingOrder) FROM faq C) +1)
Thanks
The best solution is to make sortingOrder an AUTO_INCREMENT column. The database will assign the values automatically, incrementing them for each row.
If you can't do that for some reason, you can check if the subquery returns NULL and replace it with 1.
INSERT INTO faq (question, answer, sortingOrder)
SELECT '$questionData', '$answerData', IFNULL(MAX(sortingOrder)+1, 1)
FROM faq

Mysql insert query in after a chosen row? [duplicate]

This question already has answers here:
Is it possible to insert a new row at top of MySQL table?
(5 answers)
Closed 3 years ago.
I have persons in my database with their attended trainings but as time goes by, each of them will add some trainings again so i need to add it after that name.
I have table with columns id, firstname and training but that's my problem, inserting after a selected name
Sample
Insert into table (firstname, training) values ("Zarie", "Technical Writing Course") after a selected row where id = (select id where firstname="Zarie");
Thank you.
When you adding row to mysql table it goes to the end of table or on free primary key id. You can't change this when you inserting data. You can change order only when you selecting data:
SELECT * FROM table WHERE name = 'name' ORDER BY id DESC, firstname ASC
SELECT id, firstname FROM table WHERE name != 'Zimbo' ORDER BY firstname ASC

INSERT using a SELECT statement and also add a variable value to each row of the result at the same time in MySQL [duplicate]

This question already has an answer here:
MySQL insert data with fixed values and multiple select results
(1 answer)
Closed 4 years ago.
I have a table nameValue that has 5 columns (Id,val,firstName,lastName,age). I use a select statement that gives firstName,lastName, age in multiple rows, lets say it returns 5 rows. Id is Auto-Increment. I want to add val to each of the 5 rows returned.
$val=55; INSERT INTO nameValue (val,firstName,lastName,age) VALUES($val, SELECT fName,lName,age FROM nameAge WHERE age<25); something like this where we are insert same variable $val to each of the 5 rows returned by the select statement.
Thats the wrong way. Try this -
INSERT INTO nameValue (val,firstName,lastName,age)
SELECT $val, fName,lName,age FROM nameAge WHERE age < 25

My SQL QUERY for insert into select statement gives error [duplicate]

This question already has answers here:
PHP, MySQL error: Column count doesn't match value count at row 1
(3 answers)
Closed 6 years ago.
$sql="INSERT INTO `tempahan`(`ic`,`nama`,`tarikh`,`tarikhakhir`,`mula`,`akhir`,`unit`,`bil`,`sebab`) ``SELECT ic, nama
FROM register";`
i use this to select the register column to be inserted in tempahan column.
but it gives me error 'Column count doesn't match value count at row 1'
just insert into the columns you really want to insert into - as the error states, the number of the columns need to be identical:
$sql="INSERT INTO `tempahan`(`ic`,`nama`) (SELECT `ic`, `nama`
FROM `register`)";
update: you can't use VALUES() when using a subquery to get the insert-values. I just corrected this.
Your insert statement for tempahan table provides more columns than you select from register table. Error message clearly says this. Use this:
$sql="INSERT INTO `tempahan`(`ic`,`nama`) ``SELECT ic, nama FROM register";`

Finding a specific number within a comma separated list [duplicate]

This question already has answers here:
Opposite of MySQL FIND_IN_SET
(6 answers)
Closed 9 years ago.
I have a database which has a field containing some comma separated values like 1,8,3,54,5,19,9..... I want to select only those rows where 2 doesn't exists.
The query below is used for finding all fields containing the number 2 in the attachedCompanyIDs column. However, I want to find all rows where that number doesn't exist, but I don't know how to use find_in_set in this case. Can any one please help me?
SELECT name FROM company
WHERE orderID = 1 AND FIND_IN_SET(2, attachedCompanyIDs);
SELECT name FROM company
WHERE orderID = 1 AND NOT FIND_IN_SET(2, attachedCompanyIDs);
or
SELECT name FROM company
WHERE orderID = 1 AND FIND_IN_SET(2, attachedCompanyIDs) = 0;

Categories