How to get max value of a column from two tables - mysql

I have one table named 'posts' and one table named 'threads'. Both have the column called 'total_id' which is an integer.
Now, how to get from those two tables the 1 highest value (max) in the 'total_id' column? (MySQL)

You can get it as follows:
SELECT Greatest(
(SELECT Max(total_id) FROM posts),
(SELECT Max(total_id) FROM threads)
)

Though it's not really very clear what's your expected result but in case you want max of total_id including both tables data? If yes, then you can do a UNION and then get the highest value like
select max(total_id) as max_total_id from (
select total_id from posts
union
select total_id from threads ) xxx;

Related

counting all records in table and putting it into a variable inside mysql itself

I have a table named financial_trans which consist of
id (AI,INT)
amount (DOUBLE)
acadYear (VARCHAR) // probably '2020-2021'
now I want to count the similar acadYear using group by and store it into a mysql variable and my query for that is
SELECT s.total as total
INTO #year
FROM (
SELECT COUNT(acadYear) as total
FROM financial_trans
GROUP BY acadYear
) s
but throws an error like below,
#1172 - Result consisted of more than one row
Your sub-statement SELECT COUNT(acadYear) as total FROM financial_trans GROUP BY acadYear yields more then 1 row, so it can fit into variable #year.
That basically means that you have records for more then 1 distinct year in your financial_trans table. If you need to count records only for specific year, just add WHERE acadYear = XXXX into subquery. Or even better, SELECT COUNT(1) FROM financial_trans WHERE acadYear = XXXX.
Side note: DOUBLE is not ideal column type for financial records, you should use DECIMAL

Mysql union does not sum same values

Below is my sample code for mysql
SELECT ROUND(SUM(final.spend),4) as tot_spend
FROM
(SELECT 1 as spend FROM `tab1` as tds WHERE 1
UNION
SELECT 1 as spend FROM `tab2` as obm WHERE 1
) as final
For better understanding I replace the column value as 1 in query, because value from both tables are same,If run above query I get result 1, it does not sum the value, if I change any one table value for Eg. change tab2 value as 2 and run means it shows 3, for my understanding if the value from both tables are same means SUM wont work , differ means its SUM the value from the tables, is the default one or my understanding is wrong? Anyone help to solve my issue, I need to sum any value(same or differ) from both table.
The UNION operator selects only distinct values by default. To allow duplicate values, use UNION ALL:
SELECT ROUND(SUM(final.spend),4) as tot_spend
FROM
(SELECT 1 as spend FROM `tab1` as tds WHERE 1
UNION ALL
SELECT 1 as spend FROM `tab2` as obm WHERE 1
) as final
This will give you the correct sum.

Concat two columns and columns are obtained via SELECT queries

I need help with CONCAT function. I have two select queries and the result of every query is one column. I need to merge this two columns in one. Is that possible? Beacuse, I can't get result even if I try with simple select queries like:
SELECT owner FROM table WHERE number="value1";
SELECT number FROM table WHERE owner="value2" AND number IS NOT null;
These queries work and throw 3 rows like result. But, if I want to merge them in one column using CONCAT - that doesn't work. Do you know why?
SELECT CONCAT(SELECT owner FROM table WHERE number="value1",
SELECT number FROM table WHERE owner="value2" AND number IS NOT null
) as NEW_COLUMN FROM table;
I think you want this:
SELECT CONCAT(owner, number) newCol1
FROM yourTable
WHERE number="value1"
OR (owner="value2" AND number IS NOT null)
SELECT
CONCAT(owner, number) as NEW_COLUMN
FROM
table
WHERE
owner = "value2"
AND number = "value1"
AND number IS NOT NULL
The fundamental reason is that the DB cannot concatenate two different SELECTs which might have a different number of rows.
What you need to do is to re-formulate your query in terms of a JOIN.
For example suppose we have this table:
owner number
John value1
value2 123456
Your first query:
SELECT owner FROM table WHERE number="value1";
will return "John". The second one
SELECT number FROM table WHERE owner="value2" AND number IS NOT null;
will return "123456".
If you CONCAT the two values you would therefore get "John 123456".
First of all, is this the expected behaviour of the query you want? What happens is there is a third row with owner=Jack and number=value1, so that the first query returns TWO rows "John" and "Jack"?
One thing you could look into is the CROSS JOIN syntax.
SELECT CONCAT (table1.owner, ', ', table2.number) AS new_column
FROM ( SELECT owner FROM table WHERE number="value1" ) AS tablel1
CROSS JOIN
(SELECT number FROM table WHERE owner="value2" AND number IS NOT null ) AS table2;
Note that if the first query returns three rows and the second four rows, the combined query will return 3*4 = 12 rows.

How to retrieve a count number of specified values in mysql (even if no records)?

Suppose I have a table with a field named 'rating', it may take different values, but I want to receive a count of specific values.
Example:
Create table mytable(
rating int(1),
);
First and the obvious way I could think of was the following:
select rating,count(rating) from mytable group by rating order by rating
The problem though it is not clear how many values it would return, it may be also not easy to process them way.
What I would really like to do is to select two fields in one row showing the number of records that have some specific values.
Example...
//something like this (some pseudocode):
select count(rating=-1) as rating1, count (rating=1) as rating2 from mytable
Could you advice on some neat way I could select in the ^ above format?
select SUM(IF(rating=-1,1,0)) AS rating1,
SUM(IF(rating=1,1,0)) AS rating2 from mytable

Fast MAX, GROUP BY on the concatenation of mulliple columns

I have a table with 4 columns: name, date, version,and value. There's an composite index on all four, in that order. It has 20M rows: 2.000 names, approx 1.000 dates per name, approx 10 versions per date.
I'm trying to get a list that give for all names the highest date, the highest version on that date, and the associated value.
When I do
SELECT name,
MAX(date)
FROM table
GROUP BY name
I get good performance and the database uses the composite index
However, when I join the table to this in order to get the MAX(version) per name the query takes ages. There must be a way to get the result in about the same magnitude of time as the SELECT statement above? I can easily be done by using the index.
Try this: (I know it needs a few syntax tweaks for MySQL... ask for them and I will find them)
INSERT INTO #TempTable
SELECT name, MAX(Date) as Date
FROM table
Group By name
select table.name, table.date, max(table.version) as version
from table
inner join #TempTable on table.name = #temptable.name and table.date = #temptable.date
group by table.name, table.date