Fetching empty column with where clause MYSQL [duplicate] - php

This question already has answers here:
How do I check if a column is empty or null in MySQL?
(21 answers)
Closed 3 years ago.
I am creating a table where I want to show the data where one column is empty.
I tried everything but nothing seems to work
$sql = "SELECT * from tblleaves where empid=:eid and EndKm is NULL order by PostingDate desc";
In other words, I want to fetch data where endkm column is empty or has spaces only.
If I remove EndKm is NULL everything works fine for me.

Try SELECT * from tblleaves where empid=:eid and (EndKm IS NULL or TRIM(EndKm) = '') order by PostingDate desc

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

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).

Fetching ALL data from Table with Where AND OR [duplicate]

This question already has answers here:
OR WHERE and AND in mySQL Query
(2 answers)
Closed 3 years ago.
I am new to this forum. I hope I will be able to explain my question to everyone.
I want to call the data from columns (WareParkOut or/and EndKm) with specific user. But the query I am using in my session is showing the data from other user id's too
$sql = "SELECT *
from tblleaves
where empid=:eid
AND WareParkIn = ''
or EndKm = ''
order by PostingDate desc";
I am finding it hard to use AND or operators here which I believe is the problem.
AS a result, I want to call everything from table tblleaves in every customer login if WareParkIn is empty or EndKM is empty or both are empty. But it is showing all customers empty data in every login. I want it login specific.
e.g User ID 1's login is showing other users WareParkIn or EndKM too
$sql = "SELECT * from tblleaves
where empid=:eid
AND (WareParkIn = '' or EndKm = '')
order by PostingDate desc";

Show only rows that contain data in one specific column [duplicate]

This question already has answers here:
MySQL select where column is not empty
(13 answers)
Closed 8 years ago.
I have a table that contains thousands of rows. I'm trying to show only rows that have information in a specific column. The column name is "vhf" the table name is "p_loc". The majority of the rows contain no info in the column "vhf" but, when I try the code below it wants to echo all my rows.
I just need the rows that contain any information in the column "vhf"
$sql = mysql_query("SELECT vhf FROM p_loc WHERE vhf IS NOT NULL");
Sounds like there are two cases you want to filter out:
the vhf column is NULL
the vhf column is empty, or an empty string
This means you need two clauses in your SELECT:
$sql = mysql_query("SELECT vhf FROM p_loc WHERE vhf IS NOT NULL AND vhf != ''");

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