MySQL Subqueries - mysql

I was wondering if something like this is possible in MySQL:
SELECT * FROM tmpTable WHERE id = 1 AND ISTRUE(UPDATE tmptTable SET value = 1)
Or
SELECT * FROM tmpTable WHERE id = (INSERT INTO tmptTable (name) VALUES ('test'))
Or the same query with UNION or something
What i'm trying to do with this is SELECTing only the records that are updated/by the ID's they are inserted. I want to know if it's acutally possible to do an Update or Insert query INSIDE an other query.

Looks like you are trying to do composable DML. No this isn't supported in MySQL. – Martin
https://stackoverflow.com/users/513811/martin
He just gave the answer, but as a comment...

Related

php prepared statement multiple possible querys

I'm using prepared statement for MySQLi PHP which is all working. However, the application that I'm using this in has 108 different possible statements all very similar that can be run either without condition:
select * from table1
or with
select * from table 1 where user_level = 1)
The question that I'm asking: Is there a way that I can create the statement that covers all possibilities such as
select * from table 1 where user_level = {special input}
that will give the same outcome as
select * from table1
Otherwise I'm looking at a lot of repetition.
You would use parameter binding. See https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php.

How can I implement a "complex" INSERT query that retrieve the values calling other queries?

I am not so into database and maybe I am saying something trivial.
I am working on MySql and I have to implement a "complex" INSERT query.
I mean that I have to do something like this:
INSERT INTO MarketDetails_CommodityDetails
(market_detail_id, commodity_detail_id) VALUES
( XXX, YYY)
where XXX and YYY are not simple values but are the results of two SELECT queries both returning a single value (XXX is return by a SELECT query and YYY by another query).
I know that I can perform these queries, store the output in a variable and then call my insert query passing these parameters but I am asking if there is a way to automatically do it into my INSERT query.
Why do you want to use insert values ?
just perform an insert select
INSERT INTO MarketDetails_CommodityDetails(market_detail_id, commodity_detail_id)
select * from
(select --Complex select for XXX)
cross join
(select --Complex select for YYY)
You could also use values if you wanted to. Make sure you use 2 parenthesis instead of just 1 and make also sure to use limit statement even if your query always returns 1 row.
INSERT INTO some_final_table (column1, column2) VALUES (
(SELECT some_column_1 FROM some_middle_table_1 LIMIT 1),
(SELECT some_column_2 FROM some_middle_table_2 LIMIT 1)
)

How to get single record by search query and skip other records?

To get a record I normally use this kind of query:
select * from tablename where columnName = 'abc'
This query search for all abc in the table. However I want just to get only first record that has abc and don't look for other.
Please suggest me sql query for this purpose.
This will just return the first instance as it scans the table.
select * from tablename where columnName = 'abc' limit 1;

Querying mysql workbench for specific attribute

in mysql workbench, I want to run:
SELECT * FROM testdatabase.testTable;
But I want to restrict this to entries in the table that only have a specific attribute value, like "userId"=123
How would I do this?
Thank you.
Just use a WHERE clause:
SELECT * FROM testdatabase.testTable WHERE userId = 123;
This will select all rows from your table that have the userId 123.

I have a mysql table contains lot's of records

I have a mysql table contains lot's of records. my table has a varchar field and a timestamp field. (I have one record for every minute)
I want to select records like this:
1,3,5,7,9,11,...
or 1,4,7,10,13,..
or something like this.
I can get done it using php while function, but it is not a good solution. is there any mysql select parameter to get it exactly from mysql?
p.s: sorry for post title, this is the only title stackoverflow accept it.
select * from table where identity_column %2 <>0 -- to select 1,3,5,7,9...
and for your 2 condition do this !
select * from table where identity_column%3 =1 -- to select 1,4,7,10,13,....
For selecting records like 1,3,5,7,9,11,etc. You can do this:
SELECT *
FROM TableName
WHERE autoIncreamentField % 2
NB: Not necessary to check where clause against 0 or 1. It will select records if where clause returns 1. An example in Fiddle.
For records like 1,4,7,10,13,etc. You can do:
SELECT *
FROM TableName
WHERE (autoIncreamentField % 3)=1
select * from table order by rand()