I have a complicated MYSQL query question here. I try my best to explain my problem.
I have 4 tables. mid is a foreign key between the tables. table4 is NOT a compulsory table source. However I like it returns all the rows even there are no match data from table4. So that I write a query script as following.
I'm not sure is it the logic way to write such query script but what I know that the syntax is wrong.
SELECT *
FROM table1, table2, table3,
(SELECT xx
FROM table4
RIGHT JOIN table1 ON table1.mid = table4.mid)
WHERE table1.mid = table2.mid
AND table1.mid = table3.mid
AND tt = 'a'
AND type = 1
GROUP BY table1.mid
ORDER BY xx DESC, table1.name ASC;
You have to do a left join between table1 & table4:
SELECT *
FROM table1
JOIN table2 ON table1.mid = table2.mid
JOIN table3 ON table1.mid = table3.mid
LEFT JOIN table4 ON table1.mid = table4.mid
WHERE tt = 'a'
AND type = 1
GROUP BY table1.mid
ORDER BY xx DESC, table1.name ASC;
Related
I have a query that gets website visit logs from a table. I then join a further 2 tables to this in order to get all of the user's information. At this point the query is fine.
Slow Query:
I then have to use a sub query in order to get the visit logs as of a certain date based on a condition. It is this sub query that causes the entire query to practically grind to a halt. After 30-40 seconds the query will finish.
Tables:
The tables are InnoDB (changing to MyISAM made no difference to performance).
Table1 has close to 1m records. Table2 has around 250K. Table3 has around 100K. Table4 has around 500K.
Query:
SELECT COUNT(*) AS visits , table1.userName, table2.userId, table2.col1, table2.col2, table2.col3, table2.col4, table3.col20
FROM table1
LEFT JOIN table2 ON table2.userName = table1.userName
LEFT JOIN table3 ON table3.col2 = table1.col2
WHERE table1.col1 = 'foo'
AND table1.Date > (
SELECT max(a.VisitDate) FROM table4 a
WHERE a.userId = table2.userId AND a.col1 = 'bar'
)
GROUP BY table1.userName, table1.Date
I did not build this data structure and have no option to change it. I am parsing this query into a basic PDO function.
You can try using a inner join (on subselect)
SELECT
COUNT(*) AS visits
, table1.userName
, table2.userId
, table2.col1
, table2.col2
, table2.col3
, table2.col4
, table3.col20
FROM table1
LEFT JOIN table2 ON table2.userName = table1.userName
LEFT JOIN table3 ON table3.col2 = table1.col2
INNER JOIN (
SELECT max(a.VisitDate) as max_visit_date FROM table4 a
WHERE a.userId = table2.userId AND a.col1 = 'bar'
) t on table1.Date > t.max_visit_date
GROUP BY table1.userName, table1.Date;
I have 3 tables: table1, table2 & table3
I make a select query from table1 which LEFT JOINS the other two tables. In the select I have a group_concat which takes a value from table3. Everything works well until a row with a specific row doesn't exist. The group_concat list becomes empty. Instead, I would like it to set values in the group_concat to NULL for the ones where the rows doesn't exist.
Like I said if the value in table3 exist for all the rows in table2 then it works. If not, the whole group_concat is empty.
Some "simplified" code of what I got so far:
SELECT
table1.table2Id,
table1.dateAdded,
IF(COUNT(table2.table3Id) = COUNT(*), GROUP_CONCAT(table2.table3Id), NULL) as group1,
IF(COUNT(table3.ext) = COUNT(*), GROUP_CONCAT(table3.ext), NULL) as group2
FROM table1
LEFT JOIN table2 ON
table2.id = table1.table2Id
LEFT JOIN table3 ON
table3.id = table2.table3Id
Fixed it by changing
IF(COUNT(table3.ext) = COUNT(*), GROUP_CONCAT(table3.ext), NULL) as group2
to
GROUP_CONCAT(IFNULL(table3.ext, NULL)) as group2
In your situation when you are using joins use derieved sub query in join and use IFNULL and set its default value to 0 then in the outer table this value (0) will be used if there comes null.
EDITS :
as there is no data to test you can do it like this. Use INNER JOIN instead of left join.
SELECT
table1.table2Id,
table1.dateAdded,
IF(COUNT(table2.table3Id) = COUNT(*), GROUP_CONCAT(table2.table3Id), NULL) as group1,
IF(COUNT(table3.ext) = COUNT(*), GROUP_CONCAT(table3.ext), NULL) as group2
FROM table1
INNER JOIN table2 ON
table2.id = table1.table2Id
INNER JOIN table3 ON
table3.id = table2.table3Id
Also try using derieved sub query
SELECT
table1.table2Id,
table1.dateAdded,
IF(COUNT(t2.table3Id) = COUNT(*), GROUP_CONCAT(t2.table3Id), NULL) as group1,
IF(COUNT(table3.ext) = COUNT(*), GROUP_CONCAT(table3.ext), NULL) as group2
FROM table1
LEFT JOIN (
SELECT
id,
IFNULL(table3Id,0) as table3Id,
table3Id
FROM table2
GROUP BY id table3Id
)as t2 ON t2.id = table1.table2Id
INNER JOIN table3 ON table3.id = t2.table3Id
I am trying to JOIN 3 Tables(table1, table2 & table3) in Mysql query where I want to pull the matching data from 2 tables(table1 & table2) comparing a Common Column existing in 3 tables(ie. 'PID').
When Joining these 3 tables, there is no data in table1 with the given Date('2012-12-27') then it's returning complete blank row.. Here, I want to get the matching data from the table2 matching the given Date and 'ZERO' or 'NULL' where there is no matching data in the other table ie. table1.. instead of a whole blank row.
Here is the code I was trying that returns a complete BLANK ROW..
SELECT * FROM table3 b
LEFT JOIN table1 r ON r.PID = b.PID
LEFT JOIN table2 a ON ab.PID = b.PID
WHERE b.Name ='stallion' AND r.Date = '2012-12-27' AND a.Date = '2012-12-27'
;
Use two different JOIN statement then UNION them.
The rows where there is no data in table1 (r) have r.Data = NULL and are therefore filtered away by your WHERE condition. You need to add OR r.Date IS NULL to your WHERE condition or move the condition to the ON clause:
SELECT * FROM table3 b
LEFT JOIN table1 r ON r.PID = b.PID AND r.Date = '2012-12-27'
LEFT JOIN table2 a ON a.PID = b.PID AND a.Date = '2012-12-27'
WHERE b.Name ='stallion';
SELECT
count(t1.id) AS c1
FROM
table2
LEFT JOIN table1 AS t1 ON (t1.uid = table2.uid)
WHERE
table2.mode = 'ls'
GROUP BY
t1.id
c1 = 6 -> CORRECT!
SELECT
count(t2.id) AS c2
FROM
table2
LEFT JOIN table1 AS t2 ON (t2.pid = table2.id)
WHERE
table2.mode = 'ls'
GROUP BY
t1.id
c2 = 1 -> CORRECT!
SELECT
count(t1.id) AS c1,
count(t2.id) AS c2
FROM
table2
LEFT JOIN table1 AS t1 ON (t1.uid = table2.uid)
LEFT JOIN table1 AS t2 ON (t2.pid = table2.id)
WHERE
table2.mode = 'ls'
GROUP BY
t1.id
c1 = 6 -> CORRECT!
c2 = 6 -> WRONG!
How do I request both counts in one query, without getting wrong results?
I need to count two different requests at the same table (table1).
so, I'm using an alias for both request. (t1). Each alias-request is working fine alone. If I use both in the same query, i got wrong results.
count() will get you the number of records that are returned by your query. Since if you removed the counts and replaced it with * you would have 6 rows both of those counts are giving you 6.
Is there any reason why you cant use two sub selects and return the result of each of those?
So:
SELECT subQ1.c1, subQ2.c2 FROM
(SELECT count(t1.id) AS c1 FROM table2
LEFT JOIN table1 AS t1 ON (t1.uid = table2.uid)
WHERE table2.mode = 'ls') as subQ1,
(SELECT count(t2.id) AS c2 FROM table2
LEFT JOIN table1 AS t2 ON (t2.pid = table2.id)
WHERE table2.mode = 'ls') as SubQ2;
I believe your problem on the full query is your group by function. You are grouping by t.id, thus a1.id will have a different count based on how many rows you have.
What I mean by this is if there are 6 rows in table t, then count is going to return 6 for table t; but also since there looks to be a 1 to 1 relation on table a, there are 6 matching rows in table a to the 6 matching rows in table t. such that
t.id = a.id
1 = 1
2= 2 ...etc.
Thus your count is returning rows versus the count you believe you should have? I believe sum function is what you want to use here.
You could try this...but I'm not really sure what you're trying to do.
SELECT (...)
count(CASE WHEN t1.uid = t3.uid THEN t1.id ELSE NULL END) AS CBanz,
count(CASE WHEN ta1.pid = t3.id THEN a1.id ELSE NULL END) AS CBanz1
FROM
t0
LEFT JOIN (...)
LEFT JOIN t1 ON (t1.uid = t3.uid)
LEFT JOIN t1 AS a1 ON (a1.pid = t3.id)
WHERE (...)
What would be a syntax of the following query:
Get all columns from Table1 and JOIN Table2 if matching reference (Table1ID) exists, otherwise JOIN Table3.
Simplified DB structure is more or less as below
Table1
ID Type
1 std
Table2
ID Table1ID Title Language
1 1 Test en
Table3
ID Table1ID Title Language Flag
1 1 Other en 1
Also, I now realized that Table3 will have multiple entries that refer to single Table1.id. How to limit it to return only the latest entry (with highest id) for every result?
If you don't want an entire separate set of columns for each join, this may be what you're looking for:
SELECT *
FROM (
SELECT a.ID AS Table1ID, a.Type, b.ID, b.Title, b.Language, NULL AS Flag
FROM Table1 a
JOIN Table2 b ON a.ID = b.Table1ID
UNION ALL
SELECT a.ID, a.Type, c.ID, c.Title, c.Language, c.Flag
FROM Table1 a
LEFT JOIN Table2 b ON a.ID = b.Table1ID
JOIN Table3 c ON a.ID = c.Table1ID
JOIN (
SELECT MAX(id) AS maxid
FROM Table3
GROUP BY Table1ID
) d ON c.ID = d.maxid
WHERE b.ID IS NULL
) a
ORDER BY a.Table1ID
SQLFiddle Demo
this is one way to do it.
select table1.id, table1.type, ifnull(table2.title, table3.title)
from table1
left join table2 on table1.id = table2.table1ID
left join table3 on table1.id = table3.table1ID