MySQL and get column headers where cell equals value - mysql

I have a SQL table with many columns (40+). The first column is an ID column and the rest are columns which only take values of 0 or 1. Is there a query in which i can search the row and see which "cells" are equal to 1 and return the column name?
For example, say my ID is 5 and columns 4,9,23 each have a 1 for that row (all others are zero). Therefore i would want to return "Column4","Column9" and "Column23" as my result. I need the ID stipulation in there too so I know which row to examine.
Any ideas? Thanks!

Related

How to compare multiple rows of the same column in the same table

I have a requirement to compare multiple rows of the same column in the same table and display one single value using SQL query. I want to display just a single row with value 1 if Accepted=Accepted and display 0 if Accepted<>Accepted.
Table I have
Table name - work
status
Accepted
Declined
Desired output
status
0

Mysql - Inserting multiple values into empty column of existing table without specifying all "where" conditions

I have a table in MySQL and I have append a new column to it with emtpy fields.
Now I am wondering if there is a possibility to append multiple rows to only this specific column without specifying the "where" condition for each row because there are many entries I wanted to so
I have something like this
INSERT INTO table.name
(`column.name`) VALUES
(2,
3,
1,
2,
0);
So that for the first row all values for each column stay the same but the value 2 will be appended to that corresponding empty column, for the 2nd row the value 3 will be attached and so on.
So I wanted to add for ALL rows a corresponding value in this new column. So the first value is for the first row, the second value of this new column for the 2nd row and so on
Thank you in advance!

Truncating rows based on column value in mysql

Let's say I have have a MySQL table with many rows that I want to limit in a specific way. Each row has a column, userID, that is indexed but is not unique. Each row also has a datetime column for createDate.
I'd like for the table to never contain more than X number (say, 1000) of rows for a given userID. So, anytime a row is inserted, if that means that there are now greater than X rows where userID = Y then the DB would delete a row based on the value of createDate.
Is this possible to achieve inside the database only? What performance concerns should I have about this kind of approach?

Is there a name for a single table space in MySQL?

In MySQL, I understand that in a table, field is used to refer to a column and record is used to refer to rows.
But is there a name for the intersection between a record and a field? I am talking about a singular value of either a record or field. In Excel, it is called a cell.
i.e.:
Column 1 Column 2
Row 1 Row 1, Column 1 Row 1, Column 2
Row 2 Row 2, Column 1 Row 2, Column 2
Row 3 Row 3, Column 1 Row 3, Column 2
What is row 1, column 1 called?
I'd go with Element or Value as the most apt.
There is no equivalent notion in SQL because a table is unordered collection of rows. So there are no cells as fixed spaces to be filled with values. For example
if a table has two equal rows those rows can not be distinguished. Sql has rows, columns and values but no cells.

updating all rows after specific record

I have a table containing an int column named "place". The values from the column determine the ordering of rows from the table on a page. It is for a blog like application. I want a query where I can update the 'place' column for all rows, including the current row, and all rows after the same.
Thanks for any help!
update topic set place = place + 1 where place >= 5