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

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;

Related

How to select all columns except some columns from a database MYSQL? [duplicate]

This question already has answers here:
SELECT * EXCEPT
(15 answers)
Closed 1 year ago.
Suppose I have a table that contains "200" columns, how do I select all colons except a few, and the way I know is to select it manually:
"SELECT id, user, name, email, city... FROM table WHERE id = 1";
However my wish would be something like:
"SELECT * (exeto essas tabelas) FROM table WHERE id = 1";
The only way you can achieve that in MySQL is to use hidden columns (https://dev.mysql.com/doc/refman/8.0/en/invisible-columns.html).

Count field other than id field [duplicate]

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

How to SUM two columns in a SELECT query in mysqli [duplicate]

This question already has answers here:
Sum Two Columns in Two Mysql Tables
(2 answers)
MySQL SUM multiple columns
(1 answer)
Closed 4 years ago.
So my question is how to SUM two columns in a SELECT query in MySQL.Here is the code I have written:
$qry = "SELECT supplier,
SUM(pay) as paid,
SUM(remaining) as remain
FROM managment
WHERE supplier = '".$_GET['ssupplier']."'";
But it only plus the column named remaining but not the pay columns.I have 2 pay columns with two values (10 and 50) but after SUM the column I am getting 1050 instead 60.Kindly take a look and help me with this problem.I want this in one table, not two tables
Thanks

How delete same value in Mysql [duplicate]

This question already has answers here:
How to delete duplicates on a MySQL table?
(25 answers)
Closed 6 years ago.
I want to delete same value in topic field and keep first row of value.
such as
no topic
1 1234
2 1234
3 1234
no = autoincrement
output
no topic
1 1234
This my code
$sql ="DELETE FROM data
WHERE no IN (SELECT *
FROM (SELECT no FROM data
GROUP BY topic HAVING (COUNT(*) > 1)
) AS A
)";
This code delete first value but I want to delete all same value and keep first value like example.
try this
DELETE FROM data
WHERE no NOT IN (SELECT no FROM
(SELECT MIN(no) as no,topic FROM data
GROUP BY topic
)NotDelete
);
sqlfiddle

SQL Select -- Checking if value is in a comma separated list -- PHP [duplicate]

This question already has answers here:
Query with multiple values in a column
(4 answers)
MySQL search in comma list [duplicate]
(5 answers)
Closed 7 years ago.
I'm wanting to create a sql select statement that will grab rows if a given value is in a comma separated list in one of the columns of the database table.
Example Table...
id | courses
1 | 5, 8, 15, 19
I want to do something like this
$course_num = 5;
$sql = "SELECT * FROM courses WHERE $course_num IS IN courses";
1.) I don't think the "IS IN courses" part is legit. How can I do this?
2.) For my code above, I would want to return the row because of the "5" in courses, not because of the "15". So, if $course_num = 9 no rows should be returned.
Thanks for your help.
By adding comma in searched occurrence
SELECT *
FROM courses
WHERE concat(', ',course,',') like '%, 5,%'

Categories