SQL check if value exists in database - mysql

I keep searching but I can only find answers on the existence in a table but that is not what I am looking for.
I want to know how I would check the existence of a specific keyword inside the entire database.
I tried selecting from every table but got an error:
SELECT * FROM dvd_drives, compatible WHERE mpn = "700577-1C6"
#1052 - Column 'mpn' in where clause is ambiguous
I can use search inside phpmyadmin but how can I use this in a query?

SELECT EXISTS ( SELECT NULL
FROM table1_name
WHERE column_name = 'value'
UNION ALL
SELECT NULL
FROM table2_name
WHERE column_name = 'value'
UNION ALL
... ) AS check_result;
SELECT EXISTS ( SELECT NULL
FROM table1_name
WHERE column_name = 'value' )
*
EXISTS ( SELECT NULL
FROM table2_name
WHERE column_name = 'value' )
*
... ) AS check_result;
The queries checks the existence only (as required in the question), rows with specified value are not returned.
1st query check does the value is present in at least one of the tables, 2nd checks that it is present in each table at least once.
PS. NULL may be replaced freely with any literal value - zero, one, some string literal, etc...

Related

Add alias to the column name where have postfix in column name in sql

Is there any possible way I can find and set the column name by giving alias
for example
I have a sql queries which contain 4 column name fields. 3 fields are common in all the queries
id, name, field
and there is another field which column name get change every time but the only common thing in that field it has a postfix as __type
so my sql query looks like this
SELECT * from table_name
id, name, field, system_data__value
is there any possible way I can add alias to the name where I found __type as type
so if I run my queries then it look like this
SELECT * from table_name
id, name, field, type
You may use UNION ALL for to set the aliases to the columns posessionally.
You must know some value which cannot present in some column (id = -1 in shown code) for fake row removing.
SELECT *
FROM (
SELECT -1 id, NULL name, NULL field, NULL alias_for_column_4
UNION ALL
SELECT * from table_name -- returns id, name, field, system_data__value
) subquery
WHERE id > 0 -- removes fake row
It is possible that the values in fake subquery needs in explicit CAST() calls for correct datatype of the output columns setting.

Selecting NULL values of the particular date

You can get the particular data if date exist in a table row, what "IF" it's "NULL"?
Can you get all NULL data of today? Is it possible using mysql query?
Edited:
I have a question, can you retrieve data that are not recorded for a certain date?
For example:
Janice Student 2017-xx-xx
Paul Student 2017-xx-xx
Mika Student ------
You can retrive those records with date, but can you retrive data that have no values? Is it possible using mysql query? or conditional x looping statement using PHP?
To locate information where there is no value stored use IS NULL e.g.
select * from yourtable where column_name IS NULL
If the column is a string of some type then it might be an "empty string", which looks like no value but isn't NULL, so for this use:
select * from yourtable where column_name = ''
or in combination:
select * from yourtable where ( column_name IS NULL OR column_name = '' )

How to return NULL when result is empty?

I have a simple query that selects one field and only one row, thus one value.
Is there any way to make it return NULL if the query results in an empty set? Instead of returning zero rows?
I think I need to use something with NOT EXISTS, THEN NULL but not certain about it.
select
(Your entire current Select statement goes here) as Alias
from
dual
dual is a built in table with a single row that can be used for purposes like this. In Oracle this is mandatory. MySQL supports it, but you can also just select a single value without specifying a table, like so:
select
(Your entire current Select statement goes here) as Alias
In either case you're selecting a single value. This means that:
If your select returns one value, that value is returned.
If your select statement returns one column, but no rows, NULL will be returned.
If your select statement returns multiple columns and/or multiple rows, this won't work and the query fails.
An easy way to do this is with aggregation:
select max(col)
from t
where <your condition here>
This always returns one row. If there is no match, it returns NULL.
Late reply but I think this is the easiest method:
SELECT
IFNULL((SELECT your query), NULL)
Use a UNION with a NOT EXISTS(original where clause)
select col1
from mytable
where <some condition>
union
select null
where not exists (
select * from mytable
where <some condition>)
You can use COALESCE for example:
SELECT COALESCE(Field1,NULL) AS Field1 FROM Table1
Edit 1:
sorry i mistake with return field as null not result set,for result set return as null use Union and Exist Function like this:
SELECT NULL AS Field1 FROM Table1 WHERE not EXISTS(SELECT Field1 FROM Table1 WHERE Field2>0)
UNION
SELECT Field1 FROM Table1 WHERE Field2>0

selecting column names where data is not null or blank

SQL Server 2008
I have a table MyTable with columns A, B, C, D
When I select a row I want a list of only those columns with non-null/blanks. The result set would be
A
C
D
if B was null in my row.
Actually, there may be a column E someday. But I can get all possible column names from another table and need to check if MyTable has any of them and if so which ones have data for the row I selected
Thus:
select * from MyTable where ID = 6
select ColumnName from AllColumnNames
For each ColumnName in the result
if ColumnName exists in MyTable AND there is data in it where ID = 6, add ColumnName to output.
There's gotta be a way to do this in one query?
This will convert your table to XML in the CTE and then it uses XQuery to find the node names that does not have empty values. This will work if your column names does not break the rules for XML node names.
;with C(TableXML) as
(
select *
from MyTable
where ID = 6
for xml path('T'), elements xsinil, type
)
select T.X.value('local-name(.)', 'sysname') as ColumnName
from C
cross apply C.TableXML.nodes('/T/*') as T(X)
where T.X.value('.', 'nvarchar(max)') <> ''
Try here: https://data.stackexchange.com/stackoverflow/query/59187
Add this the the where clause if you want to exclude the ID column as well.
T.X.value('local-name(.)', 'sysname') <> 'ID'

Sql select query optimization

I have a table and it's structured the following way:
CREATE TABLE `table` (
`type` int(11) NOT NULL,
`value` int(11) NOT NULL,
+ Some additional fields
KEY `type` (`type`)
)
What's the most efficient way to write the following query: Select the item with the maximal value of some certain type, let's say 'type0'.
If it's possible, could you also explain, what happens beneath the query (I mean, something that can affect the resulting algorithmic complexity).
I think it's
SQL Server:
SELECT TOP 1 *
FROM 'table'
WHERE type = 'type0'
ORDER BY 'value' DESCENDING
MySQL:
SELECT *
FROM 'table'
WHERE type = 'type0'
ORDER BY 'value' DESC
LIMIT 1
I guess. The most important part is that you have index on both 'type' and 'value'
Thanks #Andrew for pointing he is asking for MySQL. Cheers.
Finding just one row (with maximal value):
SELECT *
FROM tableX
WHERE type = 'type0'
ORDER BY value DESC
LIMIT 1
Finding all rows with same (maximal) value:
SELECT *
FROM tableX
WHERE type = 'type0'
AND value =
( SELECT MAX(value)
FROM tableX
WHERE type = 'type0'
)
And index on (type, value) (for InnoDB table) or on (type, value, PK) (for MyISAM table) will be useful.