Mysql can't join two tables columns into one show - mysql

I want to show two columns summarize data.
table1 - count all fields that the id same as the id on the show_users table.
table2 - sum all values that the id same as the id on the show_users table.
This is my query:
SELECT show_users.id, COUNT(`table1`.id) as sum_fields , SUM(`table2`.count) as count_all
FROM `show_users`
LEFT JOIN `table1` ON `show_users`.id = `table1`.id
LEFT JOIN `table2` ON `show_users`.id = `table2`.id
GROUP by show_users.id
ORDER BY sum_fields DESC
The table2 results are fine, but the table1 count isn't correct values...
Why is that?

SELECT show_users.id, COUNT(DISTINCT `table1`.id) as sum_fields , SUM(`table2`.count) as count_all

Related

JOIN Only if One Matching Record in both tables

Essentially I need to only LEFT JOIN on the 2 tables if there is one customerid matching in table2. If there is more than 1 record matching table2 it should not count as a match.
Currently I am doing the following:
SELECT DISTINCT
table1.customerid,
table1.name AS customer,
table2.locationid,
table2.locationname
FROM
table1
LEFT JOIN table2 ON table1.customerid = table2.customerid
WHERE
ORDER BY
name ASC
The issue is it matches on all records.
To clarify -- if customerid is in table2 more than once, it should not join on the match, only if customerid is listed once for a record in table2.
How can this be done?
Join with a subquery that only returns customer IDs that have count = 1.
SELECT DISTINCT
table1.customerid,
table1.name AS customer,
table2.locationid,
table2.locationname
FROM table1
LEFT JOIN (
SELECT customerid, MAX(locationid) AS locationid, MAX(locationname) AS locationname
FROM table2
GROUP BY customerid
HAVING COUNT(*) = 1
) AS table2 ON table1.customerid = table2.customerid
ORDER BY name ASC
Note that using LEFT JOIN means that if locationid is not in table2 at all, the query will still return a row for that location, with NULL in the table2 fields. And with this change, it will also return those null rows when there's more than 1 matching row. If you want those rows omitted from the results entirely, use INNER JOIN rather than LEFT JOIN.

mysql group by return all the rows

I have two tables (table 1 and table 2). Table 1 consists the options and table 2 consists the result for each options.
**table1** table2
id id
optionvalue optionvalueid
time (unixtime)
So when the data is inserted it will be stored in table2. There are 5 optionvalue in table 1 and when data is inserted, then in the table2 it will insert the optionvalueid of table 1 and timestamp in unixtimestamp. Eaach month, I want to count the number of values for each optionvalue. Evene if there is no value for an optionvalue, I still want to see count as zero.
I did the following query but only return the value with rows with data only.
SELECT
po.id,po.optionvalue, COUNT(pr.optionvalueid) as votes,
FROM_UNIXTIME(`time`, '%m-%Y') as ndate
FROM table 1
LEFT JOIN table 2 pr ON po.id=pr.optionvalueid
GROUP BY ndate, po.optionvalue ORDER BY ndate ASC
Is there any other ways to make the query so that it will return all the options even if there is no value.
You can CROSS join table1 to the distinct months of table2 and then LEFT join to table2 to aggregate:
SELECT t.ndate, t1.id, t1.optionvalue, COUNT(t2.optionvalueid) votes
FROM table1 t1
CROSS JOIN (SELECT DISTINCT FROM_UNIXTIME(`time`, '%m-%Y') ndate FROM table2) t
LEFT JOIN table2 t2 ON t1.id = t2.optionvalueid AND t.ndate = FROM_UNIXTIME(t2.`time`, '%m-%Y')
GROUP BY t.ndate, t1.id, t1.optionvalue
ORDER BY t.ndate ASC

Sql query to Select one result per all matches

I got two tables
Table 1:
id|value
1|Tom
1|Lucy
2|Tom
2|Lucy
3|Tom
3|Lucy
3|Bard
Table 2:
id|value
1|Tom
1|Lucy
2|Tom
2|wrong
3|Tom
3|Lucy
Results should be id where all the values match in both tables:
1
Tried this:
select distinct a.id
from table1 a
join table2 b on a.id=b.id and a.value=b.value
results are
1
2
3
INTERSECT comes to mind. Or a FULL OUTER JOIN maybe. MySQL supports neither.
The easiest way I can think of in MySQL:
select id
from table1
group by id
having (id, group_concat(value order by value)) in
(
select id, group_concat(value order by value)
from table2
group by id
);
Demo: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=768cc8fb2d01c2b5219a4d56d127d117

Why does group by and sort by give 0 response

The result of this query gives me 0 rows where it should give me the 3 latest rows (grouped by Table1.Name).
Table1 has: "Name", "Timestamp", "Voltage".
Table2 has: "Name", "data".
When I delete "ORDER BY Table1.Timestamp" I do get 3 rows (as expected) but they are the 3 oldest entries in the database where I want the 3 latest.
(I have 3 Name values in Table1 and Table2 that match).
The code:
SELECT * from Table1
INNER JOIN Table2
ON Table1.Name=Table2.Name
GROUP BY Table1.Name
ORDER BY Table1.Timestamp;
You can try to perform a query like this :
SELECT t.*,t2.* from Table1 t
INNER JOIN Table2 t2
ON t.Name=t2.Name
WHERE t.Timestamp = (
SELECT MAX(t3.Timestamp) FROM Table1 t3
WHERE t3.Name = t.Name
)
You could sort and taking the top 3:
SELECT * from Table1
INNER JOIN Table2
ON Table1.Name=Table2.Name
ORDER BY Table1.Timestamp DESC
LIMIT 3
When you are using join two or more tables then don't use * in select query. Use specific column name to avoid column ambiguously in select query , you can also use Table1., Table2. but good way to use specific columns in select query.
SELECT Table1.Name,Table1.Timestamp from Table1
INNER JOIN Table2
ON Table1.Name=Table2.Name
GROUP BY Table1.Name
ORDER BY Table1.Timestamp;

How do I select two tables in 1 query?

I want to select two tables in one query but it doesn't seem to work. I have tried nested select but I just got sql_error:
Subquery returns more than 1 row
Here is my query:
SELECT `client`.`id` as client_id,
(SELECT `org`.`name` FROM `org`) as organization
FROM `client`
What is the better way to query two tables?
Here is my expected result:
client_id = [1,2,3,4,5]
organization = [x,y,z]
This will work but it isn't what you want i think.
SELECT `client`.`id` as client_id, `org`.`name` as organization FROM `org`, `client`
This will give you a cross product of all rows in org with all rows in client. Maybe you want something like this:
SELECT `client`.`id` as client_id, `org`.`name` as organization FROM `org` JOIN `client` ON `client`.`memberOf` = `org`.`id`
The JOIN will connect rows of both tables where column memberOf in table client is equal to column id in table org
You should use join queries to join two or more tables. you can visit https://dev.mysql.com/doc/refman/5.0/en/join.html
Example of the join query:
SELECT t1.name, t2.salary
FROM employee AS t1 INNER JOIN info AS t2 ON t1.name = t2.name;