Showing particular MySQL table fields for a single row - mysql

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

Related

Mysql not like and able to find a record

I've a query:
select * from table where license='92cbb46d087' and email='email#gmail.com' and comments NOT LIKE '%0da44455%';
If I remove comments onwards it returns me 1 record. Even if I change the NOT LIKE string to anything but it is not printing a record.
What I want is find any license matching a license and email and whose comments record does not contain specific payment id. Indeed '%0da44455%' or '%xxxyyxxysss%' or any random string does not exist in the table in comments column.
If comments column can have NULL values, you won't get any output from NOT LIKE and LIKE conditions.
You need to write something like this instead :
WHERE (ISNULL(comments,'')) NOT LIKE '%0da44455%'
The reason behind this is simply because these conditions don't know how to behave with null values — How do you compare null as something that doesn't exist with another thing that doesn't exist?
Lets use subquery and select case to filter out those not like comments.
select * from (select case when coalesce(comments, '') not like '%0da44455%' then 1 else 0 end as tagCom
, * from table where license='92cbb46d087' and email='email#gmail.com') t1
where t1.tagCom = 0

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

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

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');

SELECT in mysql using column number instead of name

Is there any way to do something like :
SELECT * FROM TABLE WHERE COLUMN_NUMBER = 1;
?
No, you can't. Column order doesn't really matter in MySQL. See the below question for more details.
mysql - selecting values from a table given column number
If your table has a column named COLUMN_NUMBER and you want to retrieve rows from the table where that column contains a value of '1', that query should do the trick.
I suspect that what you are trying to do is reference an expression in the select list with an alias. And that is not supported. An expression in the WHERE clause that references a column must reference the column by name.
We can play some tricks with inline views, to give an alias to an expression, but this is not efficient in terms of WHERE predicates, because of the way MySQL materializes a derived table. And, in that case, its a name given to the column in the inline view that has to be referenced in the outer query.
How I did it:
I'm trying to take (last 3 values of) column number 4 in sometable.
set #mydb=(SELECT DATABASE());
set #mycol=(select COLUMN_NAME from information_schema.columns where
table_schema=#mydb and table_name='sometable' and ordinal_position = 4);
SELECT Date,#mycol FROM sometable ORDER BY Date DESC LIMIT 3;
Of course, if Database name is known, first line could by whiped and #mydb replaced by real database name.
You can do this trick
Example:
$query="select * from employee";
$result=mysql_query($query);
$meta=mysql_fetch_field($result,0) // 0 is first field in table , 1 is second one ,,, etc
$theNameofFirstField=$meta->name; // this well return first field name in table
// now you can use it in other query
$seconQuery="select $theNameofFirstField from employee";

Is it possible in SQL to SELECT * FROM a table WHERE the column 1 = something and not the column_name = something

I would like to SELECT * FROM table where the first column is equal to a variable. It supposed that I don't know the column name.
I know I can do something like
SELECT * FROM table WHERE column_id = 1
But I can't compare the data.
How can I do that?
I found some solution with T-SQL but it doesn't interest me.
To be more accurate :
I'm developing an administration panel in my website where the "super" admin can directly modify the database. For that I can select a table and edit this table. But to do that, I'm using an only PHP script which showing all tables, we can select one and the script show all rows in the selected table. After that you select a row and you are redirected to a page where the problem is. This page can receive any table with only one row, so I want to SELECT the data contained in this row.
Images to understand:
The first one shows the tables.
The second shows the rows of a selected table.
The third shows (normally) the data of 1 row but in this picture we can see data of many rows.
selecto http://imageshack.us/g/135/selecto.png
I found a solution :
Try to explain:
First : I selected all form the specific table which was posted
$query="SELECT * FROM ".$_POST['table']."";
$result=mysql_query($query);
Second: I attributed to a variable the column name (which I didn't know)
while($fields=mysql_fetch_array($result))
{
$col = mysql_field_name($result,0);
$nb++;
}
Third: I selected data from the table where $col = id of the row
$sql = "SELECT * FROM ".$_POST['table']." WHERE ".$col."=".$_GET['idRow']."";
$result1=mysql_query($sql);
If you know how many columns there are, you could use this little trick here:
SELECT *
FROM (
SELECT null x1, null x2, ..., null xn
WHERE 1 = 0
UNION ALL
SELECT * FROM my_table
) t
WHERE t.x1 = something
In other databases than MySQL, renaming "unknown" columns would be even simpler, e.g. in PostgreSQL you could rename only the first column like this:
SELECT * FROM my_table t(x) WHERE x = something
If you don't know anything about the table
... you can quickly query the information_schema first:
SELECT column_name
FROM information_schema.columns
WHERE table_name = :my_table
AND ordinal_position = 1
A note on SQL injection
Please don't, DON'T do this. EVER:
$query="SELECT * FROM ".$_POST['table']."";
I've recently written an article about SQL injection. Every single vulnerability like yours will allow any script kiddie to dump your database, or worse.
The solution is to sanitize your input first. Ideally, you'll maintain a catalog of allowed table strings, compare your $_POST variable with those, and then concatenate the pre-defined table string into your SQL statement, NOT the user input.
I think you can use SHOW CREATE TABLE table_name to fetch the schema of the table. After that, you should already know every column.
In PHP you could do something like:
$col = 'users';
mysql_query("SELECT * FROM table WHERE $col = $something");