Is there a way to execute a query, depending on the result of a certain column?
My table has a column col1, which could be NULL. I want to check first. If col1 is not NULL, execute another query, if col1 is NULL, do nothing (or return something else).
In pseudocode it could look like this:
IF (SELECT col1 IS NUT NULL FROM `tab1`)
THEN (SELECT col1, col2, col3 FROM `tab1`)
PS: I execute those queries from PHP, so it would also be possible, to check the result of col1 with PHP, though I would prefer to use plain SQL.
the where clause is your if
select col1, col2, col3 FROM `tab1`
where col1 IS NOT NULL
Related
I have a database table. That table has 4 columns. In 3 columns members(values) want to access the 4th column value.
So here i don't want to write same query for every member. I want to write only single query. So is it possible with single query? If possible how I can know which column has given those result set?
select 4thcolumn from tablename where lstcolumn=?1 or 2ndcolumn=?2 or 3rdcolumn=?3;
Using OR is a solution (but that requires you to repeat the parameter three times):
SELECT col4 FROM mytable col1 =:myvalue OR col2 =:myvalue OR col3 = :myvalue;
One solution to shorten the query (and pass a unique parameter) is to use IN:
SELECT col4 FROM mytable WHERE :myvalue IN (col1, col2, col3)
If you want to know which column matched, then this gets longer. In MySQL you can do:
SELECT
col4,
col1 = :myvalue is_col1,
col2 = :myvalue is_col2,
col3 = :myvalue is_col3
FROM mytable
WHERE :myvalue IN (col1, col2, col3)
This adds three columns in the result set: is_col1, is_col2, is_col3; the column(s) that matched will have value 1, other(s) will show 0.
if col1 has value lorem or null
col2 - ipsum or null
col3 - dolor or null
col4 should be lorem,ipsum,dolor if there is no null inside preceding columns.
if, for example, col2 is null the result should be lorem,dolor
something like - update table set col4 = concat(col1, col2, col3) - but jumping over nulls
Is this possible using mysql?
You could use CONCAT_WS:
CONCAT_WS() does not skip empty strings. However, it does skip any NULL values after the separator argument.
update table set col4 = concat_ws(',', col1, col2, col3);
To avoid running update every time I suggest to use generated column:
CREATE TABLE t(id INT, col1 TEXT, col2 TEXt, col3 TEXT,
col4 TEXT AS (concat_ws(',', col1, col2, col3))
)
db<>fiddle demo
I have two tables, event and performer and each of them has a slug column. In addition to the slug column, the event table has columns col1, col2 and col3 and the performer table has a column called id.
What I need is a single query that takes a slug value as input and looks at the event table first. If it finds a match, it returns all the columns from the event table. If it can't find a match, then it looks at the performer table and returns just the id.
In essence, it is the equivalent of the following two queries:
select col1, col2, col3 from event where slug = ?
If there are no results in the first query run the following:
select id from performer where slug = ?
I understand the the number of returned columns should be consistent so the value for id can be null in the first case and the values for col1, col2 and col3 can be null in the second case. I can test for null to see which was the case.
I would rather not have conditionals in the query - I have a feeling that it can be done with a single query, but can't figure out how.
You could try the IF EXISTS which can all be done in one query. I know you said no conditionals, but this is an in query command.
IF EXISTS(select col1, col2, col3 from event where slug = ?)
BEGIN
select col1, col2, col3 from event where slug = ?
END
ELSE
BEGIN
select id from performer where slug = ?
END
I hope this helped!
If you don't know what are the columns(or how many columns are there) in a table, how to check whether a particular column exists or not in that table using MySQL?
For example,
A table "DemoTable" contains columns Col1, Col2, Col3 with some data. And you write a select query like,
Select col1, col2, col3, col4 from DemoTable. But col4 does not exists in the DemoTable, so it will through an error. How to check whether col4 exists in the DemoTable in the select query or before the select query? Without a procedure.
There are two tables with same name in two DB's, in those tables there are few columns with same name, while others columns have different names. For example, DemoTable has columns col1, col2, col3, col4 in one DB and in other DB, DemoTable has columns col1, col2, col3, col5, col6. But the condition is, a single select query should be used to fetch data from the table without an error, so we need to check whether that columns first exists or not. Query like Select col1, col2, col5 from DemoTable from any DB.
I want to know if this is possible without a procedure or server side calls into the database.
I am trying to insert values into a table based on a select, and other values that will be provided from the server.
The select statement will return more than one result.
I am aware of the existence of INSERT SELECT, but is there any SELECT INSERT ? or a way to insert based on the results of a select ?
thank you
Not really sure what seems to be the problem.
You can do like this:
INSERT INTO table (columns)
SELECT
column or column expression1,
column or column expression2,
…
constant or constant expression1,
constant or constant expression2,
…
FROM a set of tables/joins
WHERE …
Not necessarily in that order (columns, then constants), no. You can mix columns with constants any way you like, just follow the order of the columns you are inserting into.
Was that what you were asking about?
I don't see why an
INSERT INTO yourtable(col1, col2, col3)
SELECT col1, col2, col3
FROM yourothertable
doesn't work for you. But you could always do a SELECT INTO #temptable to save your query in a temporary table and then you could INSERT that data or manipulate it prior to inserting. This is just a long way around the original idea, though.
Am I misunderstanding your questions?
Yes. Use this query:
INSERT INTO FOO (oof, rab) SELECT (foo, bar) FROM BAR;
I think you can do this:
INSERT INTO targetTable (col1, col2, col3)
SELECT col1, col2, col3 FROM sourceTable
UNION ALL
SELECT 'something' AS col1, 'something else' AS col2, 'yet something else' AS col3 FROM DUAL;