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

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 != ''");

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 empty column with where clause MYSQL [duplicate]

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

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;

Can I get back default values for column names in a MySQL table? [duplicate]

This question already has answers here:
How to SELECT DEFAULT value of a field
(3 answers)
Closed 9 years ago.
I can get the column names for a table but is there a way to retrieve the default data value for each column?
Here is what I'm using to get the tables column names:
$q = $dbh->query("DESCRIBE tablename");
$table_fields = $q->fetchAll(PDO::FETCH_COLUMN);
print_r($table_fields);
This prints an array of column names, but I'm trying to get the default data value for each column name also.
Another option is to go to the data dictionary and find the value in Information_Schema.Columns. This allows you to limit results to a single column.
$query = <<< endsql
SELECT Column_Default
FROM Information_Schema.Columns
WHERE Table_Schema = '$yourSchema'
AND Table_Name = '$yourTableName'
AND Column_Name = '$yourColumnName'
endsql;
Try this:
$query = "SHOW FULL COLUMNS FROM tableName";
// ...
Column Default.
I hope I helped.
Just give an index after your FETCH_COLUMN. I didn't try your code but based on DESCRIBE manual it will return 6 description columns. So if you have read this you may got the solution. I have read them and I think just by adding 4th index on your code like below will be your solution.
$table_fields = $q->fetchAll(PDO::FETCH_COLUMN,4);

Categories