select COUNT with three table - mysql - mysql

I am trying to select the COUNT with three tables with one single query (with WHERE conditions).
Here is my code which doesn't work correctly.
SELECT t1.count(id) AS car_model_count,t2.count(id) AS list_item_count,t3.count(id)
FROM `car_model` AS t1
INNER JOIN `list_item` AS t2
INNER JOIN `part_item` AS t3
WHERE t1.user_id=3;

Possible by using Sub-Query OR UNION is possible to get the COUNT from multiple table.
Try this query :
SELECT
(SELECT count(*) FROM `car_model` WHERE user_id=3 ) AS car_model_count,
(SELECT COUNT(*) FROM `list_item` WHERE user_id=3) AS list_item_count,
(SELECT count(*) FROM `part_item` WHERE user_id=3) AS part_item_count;

Related

SQL query to get 2 values from table 1 and join all possible option from table 2

I want to get value from table1 and join all matching value from table2. The table1 has to be limited to 2 rows, but expecting output should own all matching values for those two ids.
How can I achieve this?
You would use a subquery:
select t1.*, t2.*
from (select t1.*
from table1 t1
limit 10
) t1 left join
table2 t2
on t1.id = t2.table1_id;
Note: This returns two arbitrary rows. Normally, you would have an order by to better specify the rows. And use order by rand() for random rows.
if you want all the value in join for only two row of table 1 you can use a subqiuery with limit 2
select b.id, a.value, b.value2, b.table1_ID
from (
select * from table1
limit 2
) a
inner join table2 on aid = b.table1_ID

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;

Count distinct values with SELECT query results with error

I have a weird situation. I need to select all data from table name with distinct values from other table.
Here is database scheme of database that I need to get distinct values:
When I run both queries without INNER JOIN they run without error but when I use INNER JOIN I got error
This is query that I used:
SELECT * FROM `todo`
INNER JOIN
SELECT `task`.`status`,COUNT(*) as count FROM `task`
ON `todo`.`id`=`task`.`id_list` WHERE `todo`.`user_id` = 43
As you can see I need to get total count of status column from other table. Can it be done using one single query or do I need to run two querys to get data...
You need to wrap the join In parenthesis
SELECT td.*, t.*
FROM `todo` td
JOIN
( SELECT `status`, SUM(status=0) as status_0, SUM(status=1) as status_1 , id_list
FROM `task`
GROUP BY id_list
) t ON td.id= t.id_list
WHERE td.user_id = 43
You can do this in one query. Even without a subquery:
SELECT ta.status, COUNT(*) as count
FROM todo t INNER JOIN
task ta
ON t.id = ta.id_list
WHERE t.user_id = 43
GROUP BY ta.status;
EDIT:
If the above produces what you want, then you probably need:
SELECT t.*, ta.status, taa.cnt
FROM todo t INNER JOIN
task ta
ON t.id = ta.id_list INNER JOIN
(SELECT count(*) as cnt, ta.status
FROM task ta
GROUP BY ta.status
) taa
on ta.status = taa.status
WHERE t.user_id = 43 ;
You seem to want a summary at the status level, which is only in task. But you want the information at the row level for todo.

MySQL SELECT Distinct on single column not entire query

On this solution the user is querying on id to display other columns in the select but query only distinct on a single column. I've tried to do this but can't get the syntax right. Here is the query but I only want distinct on DevelopmentDisplay column.
SELECT DISTINCT
`RESI`.`DevelopmentDisplay`,
`RESI`.`City`,
`RESI`.`ZipCode`
FROM
`RESI`
WHERE
`RESI`.`ZipCode` = '91263'
ORDER BY
`RESI`.`DevelopmentDisplay` ASC
Yes you can self join tables.
I see two options like this. However i would suggest you index DevelopmentDisplay column. Depending on the number of records it can get very slow.
SELECT
t1.DevelopmentDisplay,
t1.City,
t1.ZipCode
FROM
RESI t1,
(SELECT DISTINCT DevelopmentDisplay FROM RESI) t2
WHERE
t1.ZipCode = '91263' AND
t1.DevelopmentDisplay = t2.DevelopmentDisplay;
Alternatively:
SELECT
t1.DevelopmentDisplay,
t1.City,
t1.ZipCode
FROM
RESI t1,
WHERE
t1.ZipCode = '91263' AND
t1.DevelopmentDisplay IN (SELECT DISTINCT DevelopmentDisplay FROM RESI);

MySQL, merging 2 or more tables before execute SELECT DISTINCT query?

I want to calculate how many unique logins from 2 (or probably more tables).
I tried this:
SELECT count(distinct(l1.user_id))
FROM `log_1` l1
LEFT JOIN `log_2` l2
ON l1.userid = l2.userid;
But it gives me result of l1. If I didn't put l1 on li.userid (distinct), it said "ambiguous".
How do I combine the table, and then select unique login of the combined table?
EDIT:
Tested: I test the count(distinct(l1.userid)) and count(distinct(l2.userid)). It gives me different result
If you are using LEFT JOIN then you will get at least one row in the combined result for each row in l1, so the join is entirely unnecessary if you just want a distinct count. This would give you the same result as your query:
SELECT count(distinct(l1.user_id))
FROM `log_1` l1
Perhaps you want an INNER JOIN or UNION instead? A UNION will count a user if they appear in either table. An INNER JOIN will count them only if they appear in both tables. Here's an example of the UNION:
SELECT count(*) FROM (
SELECT distinct(user_id) FROM `log_1`
UNION
SELECT distinct(user_id) FROM `log_2`
) T1