my problem is that i am joining 4 tables by left join so i cant use group by rather than sub query.
but how do i get the count of main table based on specific column
any tips .
Is it possible.
Thanks in advance
I have tried this way.
SELECT * ,sql_1.trans
FROM table1 a
LEFT JOIN table b ON condition
LEFT JOIN ( SELECT count(*) trans,dentist_id as did3 FROM table1
GROUP BY dentist_id
) sql_1 ON did3=a.dentist_id
WHERE a.current_tab='pack'
Related
There are four table Bill_entry,Customer,Chit,Cash. I want to join table Customer with table Bill_entry in following query where they have common column customer_id. Goal here is by using customer_id i want to print customer_name too in one query.
i have tried but couldn't get correct syntax
Initial code before including Customer table :
SELECT Bill_entry.*
FROM (SELECT * FROM Chit UNION SELECT * FROM Cash) as t1 RIGHT JOIN
entry
ON (Bill_entry.bill_no = t1.bill_no)
WHERE t1.bill_no IS NULL
MY tries :
SELECT Bill_entry.*, Customer.customer_name
FROM ((SELECT * FROM Chit UNION SELECT * FROM Cash) as t1 RIGHT JOIN entry ON (Bill_entry.bill_no = t1.bill_no) WHERE t1.bill_no IS NULL)customer where Bill_entry.customer_id = Customer.Customer_id
Just add in another JOIN:
SELECT e.*, cu.customer_name
FROM bill_entry e LEFT JOIN
(SELECT * FROM Chit
UNION ALL -- assume you don't want to remove duplicates
SELECT * FROM Cash
) c
entry e
ON e.bill_no = c.bill_no LEFT JOIN
Customer cu
ON cu.customer_id = e.Customer_id
WHERE c.bill_no IS NULL;
Note some changes.
The UNION --> UNION ALL. I assume you don't want to remove duplicates or incur the overhead for trying to remove them.
RIGHT JOIN --> LEFT JOIN. It is usually much simpler to think about LEFT JOINs -- keep all the rows in the first table and then matching rows in the others.
The JOIN conditions are all in ON clauses, not the WHERE clause.
I have two tables 1.material_line_item and 2.item_master there is a foreign key relation between two tables (item_master_id present in material_line_item) and there is a column called item_code in item_master. So I want a Join Query to display the item_code with my current query.
I would approach this by joining the item_master table to a subquery of material_line_item which calculates the aggregates you want for each item master id value. I am selecting all columns available though you are free to choose whichever columns you want.
SELECT t1.*, t2.*
FROM item_master t1
INNER JOIN
(
SELECT item_master_id,
SUM(received_quantity) AS Total_Received_Qty,
SUM(ordered_quantity) AS Total_Ordered_Qty
FROM material_line_item
GROUP BY item_master_id
) t2
ON t1.id = t2.item_master_id
you can simply join the two tables like
select item.item_master_id, master.item_id, Sum(received_quantity),
sum(ordered_quantity) from material_line_item item
left join item_master master on item.item_master_id = master.id
group by item.item_master_id, master.item_id
Try this,
select a.item_master_id,a.Total_received_qty,a.Total_ordered_Qty,b.item_code from (select item_master_id,
sum(received_quantity) Total_received_qty,
sum(ordered_quantity) Total_ordered_Qty
from material_line_item
group by item_master_id) a, item_master b where a.item_master_id =b.item_master_id
Hope it will help.
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;
I have a problem with the correct syntax to use UNION and GROUP_CONCAT in this situation:
I have 4 tables:
base: Is the main table with a lot of columns.
mm: Is a mm table that points to the next two tables using a 'tablenames' field.
t1 and t2 that stores data related.
Records in 'base' tables can have many related records in t1 and t2 through the mm table.
I'm creating a VIEW in MySQL and I need all those related records are displayed in a single column separated by commas.
This is the base MySQL code:
SELECT base.uid, t1.nombre_es
FROM base
INNER JOIN mm
ON mm.uid_local=base.uid
INNER JOIN t1
ON mm.uid_foreign=t1.uid WHERE mm.tablenames = 't1'
UNION
SELECT base.uid, t2.nombre_es
FROM base
INNER JOIN mm
ON mm.uid_local=base.uid
INNER JOIN t2
ON mm.uid_foreign=t2.uid WHERE mm.tablenames = 't2'
Thanks in advance.
I could do it using two VIEWS, the first using the code above with the name 'viewtest", and the second with this code:
SELECT base.uid,
GROUP_CONCAT(DISTINCT vi.nombre_es ORDER BY vi.nombre_es SEPARATOR ',') as nombre
FROM base
INNER JOIN viewtest as vi
ON vi.uid=base.uid
GROUP BY uid
Now the question is ¿How can I join this two views in a single view?
You can use derived tables from queries. Next is an example of how you can use them.
SELECT GROUP_CONCAT( f )
FROM (
SELECT 1 AS f # <-- QUERY #1
UNION
SELECT 2 AS f # <-- QUERY #2
UNION
SELECT 3 AS f # <-- QUERY #3
) AS T
Basically, you can use any SELECT query as an aliased table. Then, you can apply any aggregate functions you need to that aliased 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