sql - how to retrieve values which dont belong to a table - mysql

So basically it was given to me a list of around 300 values (numbers).
And i need to modify a parameter for all of them.
I did a basic query (example below) and i only found 270 from this 300 which was given to me.
select count(*) from table where field in('1','2','3','4','5','6');
My question is, how can i see which values (in this case are 30 values) are not present on the table?
This is a live system so i shouldnt create anything there or change.
Thanks for the help in advance.

You can add another table holding your set. Let's name it set_table with one column named set_key.
Insert your set into that table; will now look like this:
set_key
----
1
2
...
Now try this
SELECT `set_key` FROM `set_table` WHERE `set_key` not in (select value from your_other_table where 1);
This should give you the keys that are in your set but not in your table.
Example:
Your set is (1,2,42)
Your table contains values with 1 and 2
The subselect select value from your_other_table will give you 1 and 2. The whole query will now look like this: SELECTset_keyFROMset_tableWHEREset_keynot in (1, 2); That'll give you (42) as result.

Are there any other records in the table besides those 300 ? If not , select count(*) from table where field not in('1','2','3','4','5','6');

Related

SQL SELECT two specific names from table not working

Name
UID
Late
Tin
ABC
0
Bob
ABC
0
SELECT * FROM `logs` WHERE Name='Tin' AND Name='Feryal'
This query returns nothing for me and only works when I want one name.
I could use the SELECT * but for this case I would like to call specific names in the query?
For this use In clause.
SELECT * FROM logs WHERE Name IN ('Tin', 'Feryal');
You can also use or clause
SELECT * FROM logs WHERE Name='Tin' OR Name='Feryal'
To add on to Amit Verma's answer.
SELECT * FROM `logs` WHERE Name='Tin' AND Name='Feryal'
Reading that SQL statement out loud, it sounds like this:
I want to select all the rows from logs where the name is equal to Tin AND the name is equal to Feryal.
You can quickly see from that statement, the reason why 0 rows are returned, it's because that is impossible! You cannot have somebody named both Tin and Feryal at the same time unless they are some bizarre super-positional being and the datatype in the table somehow allows for that.
Amit covers the rest.

SQL SELECT everthing with value 5 but not specify from which column

I have tried to select something with SQL, and I've a problem with it.
What I want:
SQL SELECT * FROM table WHERE ? = '5';
Select everything which = 5, BUT not specify from which column.
Example:
From this ""database"", you should receive the 1st and the last row.
Is that possible?
You have to list the columns but you can use in. The where clause looks like:
where 5 in (price, height)
Note: This assumes that the columns have the same type. You could get type conversion errors if they are not.
Also, given the names of the column and the data, I assume that the columns are stored as numbers. Hence, I dropped the single quotes around 5. If they are really strings, then use the single quotes.
you need to add a condition to your query with or keyword so if any of them match the row will be shown as a result
SELECT * FROM tablename WHERE price =5 or height= 5
better you list your columns by name instead of using * after SELECT

Showing particular MySQL table fields for a single row

I have a MySQL table with 8 fields/columns. Of these, 5 columns have either 0 or 1 as values. I would like to show only those fields whose value is 1 for a particular ro .
The obvious method is to run a query like this:
SELECT * FROM table WHERE field1=1 OR field2=1 OR field3=1 OR field4=1 OR field5=1 ;
This will yield a resultset containing 8 fields/columns where the conditions are satisfied.
But what I want to try is to run a query which gives a result-set containing only that fields which has 1 as value.
Is it possible?
If possible, how can I do this?
Unfortunately, I don't think this is possible.
If you think about it when reading the resultset you'll have
to for example do :
$row[0]
Which will vary from row to row with what you're trying to do.
The result of $row[0] will always be 1 but the column selected
will differ from row to row, which is pretty confusing IMO.
A solution would be to change the design of the table and set:
visibleField1, visibleField2, visibleField3, visibleField4, visibleField5
and fill the column with the name of the field you want to show.
or name the field visibleFields and fill it with "field1,field2,field5" when you want to show those three
There's probably other ways to do this design change.
You could try it like this:
SELECT id,
CASE
WHEN field1 = 1 THEN 'badge_name'
WHEN field2 = 1 THEN 'other_badge_name'
...
END AS badge
FROM your_table
You can see an example at the link below that uses a couple stack overflow badges:
http://sqlfiddle.com/#!9/189061/1

Pass number as a column name in select statement of Sql

My query is like this
select 5 from mytable_name;
Then the output is like column name 5 and the value is 5 printing as many max number of rows exists in that table.
Can anybody tell the reason why this query is working like this?
Can anybody tell the reason why this query is working like this?
You are selecting a string literal value '5' for each row in your table:
select 5 from mytable_name;
And this works fine. Because in the SELECT statement you can select:
Column reference,
Literal value like in your case.
Function.
value expression.
Select expression.
As defined by the standard SQL1:
Update:
However, If you have a column with a name is a number like in your case, you have to escape it in order to select the values in it like so:
SELECT `143` FROM Table1;
This will select all the rows in the column 143.
But, this:
SELECT 143 FROM Table1;
Will select the string literal 143 for each row found in the table.
Note that: If possible, try not to name these columns this way, it is recommended and a best practice, not to do this.
SQL Fiddle Demo
Update 2:
Note that, if you select 143 or '143', or even "143" this will select the literal value 143 not the column date. The following are the same:
SELECT 143 FROM Table1;
SELECT '143' FROM Table1;
SELECT "143" FROM Table1;
All these SELECTs won't select the data in the column, They will select the literal value 143 not the column data. See this demo:
Demo
You have to escape the column name with the two:
``
Like this:
SELECT `143` FROM table1;
Not:
SELECT '143' FROM table1'
Like what I did here:
The right Demo
1Image From: SQL Queries for Mere Mortals
from mytable
will select all rows from your table if there is no where condition that shrinks that result. and
select 5
will select the constant number 5 for every record. If you use a column name in the select part then that value will be selected for every record.
The DB engine will name the result 5 because it automatically generates a column name and 5 is the logical name for that.
You want 'SELECT * FROM mytable_name LIMIT 0,5' perhaps?
Since you don't have anything in your where clause, it is selecting all the rows from your table. The fact that you don't select any of the columns is irrelevant - you'll still get a result for each row in the table.
With the command select 5 ... you are viewing a fixed value. Same thing you run the following command: select "test", you will be displaying a fixed string.
Using ... from mytable_name you're looking for all record of this table.
With this we can conclude that for each record in the table mytable_name shows you the fixed value "5".

Mysql IN statement only find the first element in the list

I have a table named table and one of rows in table has a with a value column like this: 6,7,8,9
I want to select this row and this is the code I expected to work:
SELECT * FROM table WHERE 8 IN (column)
It doesn't work. However the code which works confuses me:
SELECT * FROM table WHERE 6 IN (column)
I tried with several values and found out that only the first element of the list is working.
How can this be?
Because you are comparing number with string. 6,7,8,9 cast to number will be 6, so it will be true when do 6 IN (column).
You need to use FIND_IN_SET function:
SELECT * FROM `table` WHERE FIND_IN_SET(8, `column`);