mySQL: select * in combination with UNIX_TIMESTAMP - mysql

Right now I'm retrieving data from my database as follows:
SELECT id, UNIX_TIMESTAMP(cdate) as myunixdate, permalink, title FROM mytable
But I would like to do it as follows, but it doesn't work.
SELECT *, UNIX_TIMESTAMP(cdate) FROM mytable
My question is, how can I combine UNIX_TIMESTAMP without having to specify all the other fields?

Are you sure you didn't try this?
SELECT UNIX_TIMESTAMP(cdate), * FROM mytable
This won't work as the * has to come first:
SELECT *, UNIX_TIMESTAMP(cdate) FROM mytable
Aliasing it will make it easier to reference in your code:
SELECT *, UNIX_TIMESTAMP(cdate) AS cdate_timestamp FROM mytable

SELECT *, UNIX_TIMESTAMP(cdate) AS my_time_stamp FROM mytable

It works for me in MySQL 6,
Are you sure the second query is the one you really try?
What version of mysql do you use?

Related

How do i get all table names who match a specific regex in mysql 8.0?

I'm trying to retrieve all tables with specific name format,
for performing union among those tables.
I'm using mysql Ver 8.0.13, and i wrote this following query for retrieving the relevant tables:
show tables LIKE REGEX '^table_.+_class$';
I couldn't figure out the correct syntax for this query :/
Afterwards i'm planning to union all those tables.
I would like to avoid writing this code since it doesn't scale nicely:
SELECT * FROM table_french_class
UNION
SELECT * FROM table_history_class
UNION
SELECT * FROM table_pingpong_class
UNION
SELECT * FROM table_math_class
UNION
SELECT * FROM table_literature_class
Can someone suggest me how to handle this issue?
Thank you
You could use INFORMATION_SCHEMA catalog:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME REGEXP '^table_.+_class$';

What is the expression that will always be true while using SQL WHERE IN comparison query

SELECT *
FROM TABLE_X
WHERE 'Value' IN (SELECT * FROM TABLE_Y)
What expression can I use instead of 'Value' so that the WHERE clause where always return true?
I am trying to do something like:
WHERE (SELECT field FROM TABLE_Y) LIKE '%Value%'
Found the solution, use EXISTS instead of WHERE IN:
SELECT *
FROM TABLE_X
WHERE EXISTS (SELECT field FROM TABLE_Y WHERE field LIKE '%Value%')
I don't know if I understood it well, it's silly to do the following, but returns all the records in TABLE_X:
SELECT *
FROM TABLE_X
WHERE 1 IN (SELECT field FROM TABLE_Y)
NOTE: ...IN (SELECT * ...) crashes because you should choose just one column, that's why I wrote field instead of *
You cannot compare your 'Value' with *, you need a specified field from TABLE_Y, for example:
SELECT *
FROM TABLE_X
WHERE (SELECT Field FROM TABLE_Y WHERE Field LIKE '%Value%' LIMIT 1) IS NOT NULL

MySQL Timestamp how to write a query to select weekdays only

I want to write mysql for filter weekday result only. I used timestamp for store dates. can anyone help me to this?
eg; SELECT * FROM mytable WHERE WEEKDAY('mydate_field')
Considering you mean excluding Sunday and Saturday.
SELECT * FROM mytable
WHERE DAYOFWEEK('mydate_field') in (2,3,4,5,6)
Try something like this:
SELECT * FROM mytable
WHERE WEEKDAY('mydate_field')<5
you can also try like this.
SELECT * FROM your_table WHERE WEEKDAY(mydate_field)>0 AND WEEKDAY(mydate_field)<6;

Union with a timediff statement in MySQL

Not sure I'm going about this correctly to begin with - I have two valid SQL select statements that I would like to run together.
The first is
SELECT * FROM mytable;
The second is
Select TIMEDIFF(time2,time1) as diff from mytable;
So I thought maybe
SELECT * FROM mytable UNION Select TIMEDIFF(time2,time1) as diff from mytable;
But of course, the second statement doesn't have the same number of columns because it isn't a separate table. Those of you awesome at this will be able to figure it out in no time, I am sure.
select TIMEDIFF(time2,time1) as diff, t.* from mytable t;
Is it what you want?

Retrieve records based on Date

I have a date column in my table.
Is there any way in mysql to retrieve record based on date excluding the time part?
I have tried appending * but does not seems to work.
SELECT * FROM myTable where filterDate="2010-08-01 *"
Is there any way to do it like this rather then filtering it by using between?
Thanks.
SELECT * FROM myTable where filterDate LIKE "%2010-08-01 %"
You can use DATE_FORMAT function of mysql
SELECT * FROM myTable where DATE_FORMAT(filterDate,'%Y-%m-%d') ="2010-08-01"
Try this
SELECT DATE_FORMAT(filterDate , '%d-%m-%Y') AS new_date FROM table_name WHERE
DATE_FORMAT(filterDate , '%d-%m-%Y') = '2010-08-01'