Select from multiple tables without using join and union - mysql

I have the following SQL query:
SELECT t1.id as id,
t1.username as username,
CONCAT_WS(' ', t2.ad,t2.soyad) as fullname,
t2.title as title,
t3.pass as password,
t3.phone_login as hat,
t2.time as date
FROM kullanici t1, kullanici_tanim t2, dial_users t3
WHERE t1.id = t2.usr_id AND t1.agent_id = t3.user_id
GROUP BY t1.id
ORDER BY t1.id ASC
This query works fine. What I'm wondering is should I use joins? What is the correct way?

For those who will have to use your code, please use joins. E.g.:
SELECT t1.id as id,
t1.username as username,
CONCAT_WS(' ', t2.ad,t2.soyad) as fullname,
t2.title as title,
t3.pass as password,
t3.phone_login as hat,
t2.time as date
FROM kullanici t1
INNER JOIN kullanici_tanim t2
ON t1.id = t2.usr_id
INNER JOIN dial_users t3
ON t1.agent_id = t3.user_id
GROUP BY t1.id
ORDER BY t1.id ASC

Related

How can I output a column from another table when using GROUP_CONCAT

With the syntax below, t3(table3) columns are not displayed but I need to get the page_ID from the table.. Any help?
SELECT t1.*,
GROUP_CONCAT(t2.page_num separator ', ')pageNR
FROM t1
JOIN t2 ON t2.pid = t1.pid
JOIN t3 on t3.ID = t1.ID
WHERE t1.ID = ?
AND t3.page_num = t2.page_num
ORDER BY t1.tag
Presumably, something like this:
SELECT t1.*,
GROUP_CONCAT(t2.page_num separator ', ') as pageNR,
GROUP_CONCAT(t3.page_id separator ', ') as pageIds
FROM t1 JOIN
t2
ON t2.pid = t1.pid JOIN
t3
On t3.ID = t1.ID AND t3.page_num = t2.page_num
WHERE t1.ID = ?
GROUP BY t1.ID;

MySQL - Where occurs at least 3 times

I have got this query, which works fine:
SELECT t1.*, t2.ip as ip
FROM table1 t1
INNER JOIN table2 t2 ON ( t2.id = t1.t2id )
ORDER BY t1.timestamp DESC
LIMIT 1000
What I would like to do is to:
1) get only those entries where the same ip occurs at least 3 times
2) group the entries by ip
So the result would look like this example:
IP TIMESTAMP
111.111.111.111 1500000000
111.111.111.111 1300000000
111.111.111.111 1100000000
222.222.222.222 1400000000
222.222.222.222 1300000000
222.222.222.222 1200000000
I have tried many approaches and I believe that this one is the closest,
but the result is 0 rows.
SELECT *, COUNT(DISTINCT ip) FROM (
SELECT t1.*, t2.ip as ip
FROM table1 t1
INNER JOIN table2 t2 ON ( t2.id = t1.t2id )
ORDER BY t1.timestamp DESC
LIMIT 1000
) AS tmp_table
GROUP BY ip
HAVING COUNT(DISTINCT ip) > 2
Please can someone shine some light on this?
Try this:
SELECT t1.*, (SELECT DISTINCT t2.ip FROM t2 WHERE t2.id = t1.t2id)
FROM t1
WHERE
(SELECT COUNT(*)
FROM t2
WHERE t2.id = t1.t2id) >= 3
Bacause in the comments has resulted table t2 with more rows for the same IP I change my query as follow:
SELECT t1.*, t2.ip
FROM t1
JOIN t2
ON t2.id = t1.t2id
WHERE
(SELECT COUNT(*)
FROM t2 tt2
WHERE t2.ip = tt2.ip) >= 3
You can see SqlFiddle
SELECT *, COUNT(ip) FROM (
SELECT t1.*, t2.ip as ip
FROM table1 t1
INNER JOIN table2 t2 ON ( t2.id = t1.t2id )
ORDER BY t1.timestamp DESC
LIMIT 1000
) AS tmp_table
GROUP BY ip
HAVING COUNT(ip) > 2
just remove distinct
You have to have the HAVING in the subquery
SELECT t1.*, t2.ip as ip
FROM table1 t1
INNER JOIN table2 t2 ON ( t2.id = t1.t2id )
INNER JOIN
(
SELECT t2.ip
FROM table1 t1
INNER JOIN table1 t2 ON ( t2.id = t2.t2id )
GROUP BY t2.ip
HAVING count(ip) > 2
) t ON t2.ip = t.ip
Try like this;
SELECT t2.ip as ip
FROM table1 t1
INNER JOIN table2 t2 ON ( t2.id = t1.t2id )
where t2.ip IN (SELECT ip FROM (
SELECT t2.ip as ip
FROM table1 t1
INNER JOIN table2 t2 ON ( t2.id = t1.t2id )
) AS tmp_table
GROUP BY ip
HAVING COUNT(*) > 2)
ORDER BY t1.timestamp DESC
LIMIT 1000

MySQL writing join on three tables

I have written the following mysql query
SELECT distinct name, date
FROM table1
JOIN table2 ON (table2.item_id = table1.item_id)
where table1.id IN (SELECT run_id FROM
table3 where table3.status = 'FAIL') and table1.createdate >= '2011-01-01'
;
I want to include another field message from table3 in my output.
I guess this might involve joining 3 tables in the query.
Please let me know how to achieve this.
There's not quite enough info to fully answer the question. We also want to know:
Does the status column come from table1 or from table3
Can there be more than one table3 record with a runid that matches a table1.id value?
If there can be, which table3.message value do you want to show?
Without knowing that info, all we can do is guess. Here is my guess:
SELECT distinct name, date, message
FROM table1 t1
INNER JOIN table2 t2 ON t2.item_id = t1.item_id
INNER JOIN table3 t3 on t1.id =t3.run_id AND t3.status = 'FAIL'
WHERE t1.createdate >= '2011-01-01'
Try this :-
SELECT DISTINCT T1.NAME , T1.DATE , T3.MESSAGE
FROM TABLE1 T1
JOIN TABLE2 T2
ON
T1.ITEM_ID = T2.ITEM_ID
INNER JOIN TABLE3 T3
ON
T1.ID = T3.RUN_ID
WHERE T3.STATUS = 'FAIL' AND T1.CREATEDATE >= '2011-01-01';
Try this
SELECT distinct name, date, T3.message
FROM table1
JOIN table2 ON (table2.item_id = table1.item_id)
INNER JOIN (SELECT run_id FROM
table3 where status = 'FAIL') T3 ON T3.run_id = table1.id
where table1.createdate >= '2011-01-01'
try this :
select distinct name,date, message from table1 t1,table2 t2,table3 t3 where t1.item_id = t2.item_id and t1.item_id = t3.item_id
and t3.status ='FAIL' AND t1.createdate >= to_date('2011-01-01,'YYYY-DD-MM');

Issues with SQL queries

I have 2 tables and result as shown in the image below: MySQL DB
What would be best way to join the two tables so we get the result as shown above.
SELECT * FROM (SELECT id, desc FROM table2) as T1
LEFT JOIN (SELECT * FROM table1) as T2 ON T1.id = T2.id
I guess my SQL is not working.
You can use a LEFT JOIN with COALESCE:
SELECT t1.id, COALESCE(t2.desc, t1.desc) AS desc, t1.D1, t1.D2
FROM table1 as T1
LEFT JOIN table2 as T2 ON T1.id = T2.id
Use a left join with coalesce to prioritize table 2's values if they are present, but fallback on table 1's values if not.
select t1.id,
coalesce(t2.desc, t1.desc) as desc,
t1.d1, t1.d2
from table1 t1
left join table2 t2
on t2.id = t1.id
order by t1.id
You can use ifnull:
SELECT t1.id, ifnull(t2.desc, t1.desc) AS desc, t1.D1, t1.D2
FROM table1 as T1
LEFT JOIN table2 as T2 ON T1.id = T2.id
coalesce or case .. when is also possible. All together with the left join

GROUP CONCAT with sum function

I want to concat the total of my sum function, please help me, Thanks
This is my query:
SELECT t1.province, t2.fullname,sum(t1.total_vote) AS total
FROM votes AS t1, candidate AS t2
WHERE t1.candidate_id = t2.id
GROUP BY t2.id, t1.province
ORDER BY t2.id
So just use concat. (And an inner join would be neater.)
SELECT concat(t2.fullname, ' has ', sum(t1.total_vote), ' votes in ', t1.province, '.') AS concatinated_line
FROM votes AS t1
INNER JOIN candidate AS t2 ON t1.candidate_id = t2.id
GROUP BY t2.id, t1.province
ORDER BY t2.id
The above answer should be correct, just add a closing parens in the SELECT:
SELECT concat(t2.fullname, ' has ', sum(t1.total_vote), ' votes in ', t1.province, '.') AS concatinated_line
FROM votes AS t1
INNER JOIN candidate AS t2 ON t1.candidate_id = t2.id
GROUP BY t2.id, t1.province
ORDER BY t2.id
Try this:
SELECT fullname , GROUP_CONCAT(cast(total as char))
FROM
(
SELECT t2.fullname fullname,sum(t1.total_vote) AS total
FROM votes AS t1, candidate AS t2
WHERE t1.candidate_id = t2.id
GROUP BY t2.id, t1.province
ORDER BY t2.id
) s