I want to show column expect column number 1
for example = select * from table; this query will show all column
column1 column2 column3 ...
but my question is how to show all column expect column1, so the result will be like this
column2 column3 ...
I don't want to use select column2, column3 from table because that query not efective if my column more than 3. thank
You can select the column after SELECT:
SELECT * FROM table
* means ALL columns, so:
SELECT columnName, columName2 FROM table
Related
I want to search for a single value in 3 columns and return each occurrence of this value.
My query is based on the following answer regarding a similar request.
SELECT *
FROM table
WHERE 'abc' IN (column1, column2, column3)
However, I don't want to return the whole row, only the single value. It possible that the value can found in multiple columns for the same row. Each occurrence should be returned and the end result should be a 1-dimensional list. How do I need to alter my query?
The value that you want returned in each row is the same as the value that you search for, so a statement like this will do:
SELECT 'abc' AS column_name
FROM table
WHERE 'abc' IN (column1, column2, column3)
If you want 'abc' returned once for each occurrence in any of the 3 columns you should use UNION ALL:
SELECT column1 AS column_name
FROM table
WHERE column1 = 'abc'
UNION ALL
SELECT column2 AS column_name
FROM table
WHERE column2 = 'abc'
UNION ALL
SELECT column3 AS column_name
FROM table
WHERE column3 = 'abc'
if column1 has 1 2 3
and column2 has 1.2 3 4.1
SELECT column1, column2
FROM table
WHERE column2 (doesn't contain ".");
is that possible ? and what is the translation of it ?
i don't want the output to show the row that has a DOT in column2
so the output would only be 2,3
Thanks !!
SELECT column1, column2 FROM table WHERE column2 NOT LIKE '%.%'
SELECT column1,column2 FROM table WHERE column2 NOT LIKE '%yourstring%';
I was wondering if you could create columns based on the result from a Select statement.
Table 1:
availableColumns
-----------------
column1 -> record 1
column2 -> record 2
column3 -> record 3
So if I did a Select availableColumns From Table1, how could I create a table that has then the following structure, the results must be used to create columns:
column1 | column2 | column3
If I try:
CREATE TABLE test SELECT availableColumns FROM table1
I get the following:
Column
------
Column1
Column2
Column3
So instead of columns, I get my result as rows which I don't want.
Thanks in advance :)
You can have a list of columns for a specific table by selecting them from Information Schema table
SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='databasename'
AND `TABLE_NAME`='tablename';
To create the table just use it
CREATE TABLE test SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='databasename' AND `TABLE_NAME`='tablename';
Just change 'databasename' and 'tablename' to fit your actual database and table
What's the easiest way to select a row, modify couple of columns and insert it to the same table?
I'm trying to insert a new row based on another.
INSERT INTO table2 (column1, column2, column3)
SELECT column1, 'no', column3 FROM table2 WHERE column2 = 'yes'
Hopefully this is a bit clearer as to how you do this. As you can see, I've grabbed two columns from table2 and for the other column I used a text value for instead of the value for column2.
Other patterns you can use:
Combine a column and some other text (Assumes the column is already a string data type.
INSERT INTO table2 (column1, column2)
SELECT column1 + 'no', column2 FROM table2 WHERE column2 = 'yes'
Combine a column and some text, One example where the column is a string and one where it is not.
INSERT INTO table2 (column1, column2)
SELECT column1 + 'no', 'A' + cast(column2 as Varchar (10)) FROM table2 WHERE column2 = 'yes'
INSERT INTO table(column1,column2) SELECT column1, const2 FROM table ...
The select list will likely mix copied columns (by name) and changed columns (by desired values).
Say column is int
INSERT INTO table(column1,column2) SELECT column1 + 1, column2 -1 FROM table
You can multiply columns and perform string functions.
I have table named x
Here is the sample data in table x
Column1____________
Column2______________
Column3
a ___________________ b______________________c
a2__________________b2______________________c
a3__________________b3______________________c2
a4_________________b4_______________________c3
The result I need in a new table is:
Column1____________
Column2______________
Column3
a3__________________b3______________________c2
a4_________________b4_______________________c3
I tried importing the values to a new table and applying a unique index on column 3 but the result i get out of it is
Column1____________
Column2______________
Column3
a__________________b______________________c
a3__________________b3______________________c2
a4_________________b4_______________________c3
SELECT * FROM x WHERE Column3 NOT IN
(SELECT Column3 FROM x GROUP BY Column3 HAVING COUNT(*) > 1)