how to use 'inner join' and 'order by' in sql - mysql

I want to use inner join and order in my query in php page.
My query :
select
*
from
table1
inner join
table1category
on table1.table1category_id = table1category.id
order by updateDate desc;
'updateDate' is for table1 => error : Column 'updateDate' in order clause is ambiguous

Column 'updateDate' in order clause is ambiguous
Means that updateDate exists in both tables you are trying to use.
If you want to order using this field, you have to specify from which table you want it to be ordered by.
For example:
SELECT
*
FROM table1
INNER JOIN table1category
ON table1.table1category_id = table1category.id
ORDER BY table1.updateDate DESC;
Also, consider that using * on a SELECT with JOINS will get all the columns from all the included tables.

when there is same column in two table then you've to specify the table name with that columns
SELECT
*
FROM table1
INNER JOIN table1category
ON table1.table1category_id = table1category.id
ORDER BY table1.updateDate DESC;

Its because updateDate exists in both tables- table1 and table1category
SELECT
*
FROM table1 t
INNER JOIN table1category tc
ON t.table1category_id = tc.id
ORDER BY t.updateDate DESC;

You have to say which table to use for this field (presents in both tables):
select *
from table1
inner join table1category on table1.table1category_id = table1category.id
order by XXX.updateDate desc;
Replace XXX by table1 or table1category.
You can set aliases on tables too, like:
select *
from table1 as myalias1
inner join table1category as myalias2 on table1.table1category_id = table1category.id
order by XXX.updateDate desc;
Then replace XXX by myalias1 or 2.
You can do the same for selecting datas (for example):
SELECT table1.id AS id, table2.id AS categoryId

Column updateDate in order clause is ambiguous
Try This
SELECT
*
FROM table1 t1
INNER JOIN table1category t2
ON t1.table1category_id = t2.id
ORDER BY t1.updateDate DESC;

I find that using aliases often makes joins easier
SELECT
*
FROM table1 AS alias1
INNER JOIN table1category AS alias2
ON alias1.table1category_id = alias2.id
ORDER BY alias1.updateDate DESC;

You are missing table name
SELECT
*
FROM table1
INNER JOIN table1category
ON table1.table1category_id = table1category.id
ORDER BY table1.updateDate DESC;

SELECT *, table1.updateDate as "Table1 Update Date"
FROM table1
INNER JOIN table1category ON table1.table1category_id = table1category.id
ORDER BY table1.updateDate DESC;
or you can keep alias in ORDER BY clause
SELECT *, table1.updateDate as "Table1 Update Date"
FROM table1
INNER JOIN table1category ON table1.table1category_id = table1category.id
ORDER BY "Table1 Update Date" DESC;
Note
Use keywords always in UPPER case
Better to use alias in SELECT CLAUSE for better understanding, otherwise it will add 1 to updateDate i.e. updateDate1.
Always use alias for same column names
Don't use all columns i.e. * , fetch only required columns

select
*
from
table1 T
inner join
table1category C
on T.table1category_id = C.id
order by T.updateDate desc;

Related

Wrong count result in mysql when joining two tables

I am trying to join two tables and get the count and grouped by specific field. However, it outputs same count values even if the other table consist only two rows. How should I fix this?
Here's my code:
SELECT tbl1.preferredDay, COUNT(tbl1.preferredDay) as count_1, COUNT(tbl2.preferredDay) as count_2
FROM tblschedule as tbl1
LEFT JOIN tblappointments as tbl2 ON (tbl1.preferredDay = tbl2.preferredDay)
WHERE tbl1.preferredDay = tbl2.preferredDay
GROUP BY preferredDay;
Here is the output but it should be [15, 0][3, 3]
Your query is based on left join it will return the same count().
This is a working query for Mysql 8:
with tbl1 as (
SELECT preferredDay, count(1) as count_1
FROM tblschedule
GROUP BY preferredDay
),
tbl2 as (
SELECT preferredDay, count(1) as count_2
FROM tblappointments
GROUP BY preferredDay
)
select t1.preferredDay, t1.count_1, t2.count_2
from tbl1 t1
inner join tbl2 t2 on t1.preferredDay = t2.preferredDay
There are two WITHs to get separately the count and then an INNER JOIN to join those results
For Mysql 5.7 and lower :
select t1.preferredDay, t1.count_1, t2.count_2
from (
SELECT preferredDay, count(1) as count_1
FROM tblschedule
GROUP BY preferredDay
) as t1
inner join (
SELECT preferredDay, count(1) as count_2
FROM tblappointments
GROUP BY preferredDay
) as t2 on t1.preferredDay = t2.preferredDay

Join with column outside subquery

SELECT t1.name as r_name, t1.values as r_values
FROM table as t1
JOIN (
SELECT SUM(amount) as amount
FROM database2.table
WHERE ids IN (t1.values)
) as t2
WHERE t1.id = 20;
I get an error, that t1.values inside the subquery is unknown column.
You need to rewrite your query and take inne where to join condition:
SELECT t1.name as r_name, t1.values as r_values
FROM table as t1
JOIN (
SELECT SUM(amount) as amount
FROM database2.table
) as t2 ON t2.ids = t1.values
WHERE t1.id = 20;
Also, you don't use amount column, so what is the point of join?
Another issue, you don't have any join condition defined.
I think you need to read about joins in SQL first :)
It seems you are trying to join database2.table to your t1 based on t1.values list.
I added group by IDs in t2 since your using aggregation function. Then, not sure what's the purpose of your sum(amount)
SELECT t1.name as r_name, t1.values as r_values
FROM table as t1
JOIN (
SELECT SUM(amount) as amount, ids
FROM database2.table
GROUP BY ids
) as t2 on t2.ids IN (t1.values)
WHERE t1.id = 20;

Why do I receive an error at this join tables?

I wrote this:
SELECT DISTINCT CATEGORY FROM T AS T1
CROSS JOIN (SELECT *
FROM T
WHERE T.CATEGORY = T1.CATEGORY
ORDER BY CATEGORY DESC
LIMIT 10)
and I receive this
"Unknown column 'T1.CATEGORY' in 'where clause'".
Why?
Update:
My purpose of this is to get 10 posts of any category.
Because T1 is not visible from within the subquery.
Your JOIN also serves no purpose and/or you probably forgot the JOIN condition.
In JOIN condition should use ON keyword
SELECT DISTINCT CATEGORY FROM T AS T1
CROSS JOIN SELECT * FROM T ON T.CATEGORY = T1.CATEGORY
ORDER BY CATEGORY DESC LIMIT 10;
If you need to get 10 posts of each category you can use a query like this:
SELECT CATEGORY, Post
FROM (
SELECT a.CATEGORY, a.Post, count(*) as rn
FROM #T a
JOIN #T b ON a.CATEGORY = b.CATEGORY AND a.Post >= b.Post
GROUP BY a.CATEGORY, a.Post) dt
WHERE rn < 11;

MYSQL Performance Tuning with group by clause

I have a query like this.
SELECT count(*)
FROM table1 e
WHERE e.column1=1
AND e.id IN
(SELECT MAX(ID)
FROM table2 A
WHERE A.column1=1
AND A.date=CURDATE()
GROUP BY A.column2);
When I run this query it is taking too much of time as I am having thousands of records. How can I tune the query to perform better.
Thanks in advance.
EDIT: column2 in table2 is id of Table1
Change in (. . .) To use join instead. Like
SELECT count(*)
FROM table1 AS e
Inner join
(
SELECT MAX(ID)
FROM table2 A
WHERE A.column1 = 1
AND A.date = CURDATE()
GROUP BY A.column2
) t2 on e.id = t2.id
WHERE e.column1 = 1
Maybe:
SELECT count(*)
FROM table1 e
WHERE e.column1=1
AND EXISTS
(SELECT *
FROM table2 A
WHERE A.column1=1
AND A.date=CURDATE()
AND A.ID = e.id);

Unique result set with inner join query

SELECT
*
FROM `catalog_webdesign_products` t
INNER JOIN tbl_member_registration t1 ont.userid=t1.fld_loginid
WHERE 1
AND t1.fld_member_category_level<9
AND t.product_img IS NOT NULL
ORDER BY RAND() LIMIT 5
In this query , I need rows with unique userid. I had tried 'group by userid' also but then product_img with null value is coming in result set.
yes you can use GROUP BY userid. but your query looks wrong here
INNER JOIN tbl_member_registration t1 on t.userid=t1.fld_loginid
^------------------------space here
try this
SELECT
*
FROM `catalog_webdesign_products` t
INNER JOIN tbl_member_registration t1 on t.userid=t1.fld_loginid
WHERE t1.fld_member_category_level<9
AND t.product_img IS NOT NULL
group by userid
ORDER BY RAND() LIMIT 5
edit:
SELECT
*
FROM (select * from `catalog_webdesign_products` where product_img IS NOT NULL) t
INNER JOIN tbl_member_registration t1 on t.userid=t1.fld_loginid
WHERE t1.fld_member_category_level<9
group by t.userid
ORDER BY RAND() LIMIT 5
For better performance you should move the condition for t1 into join statement. You problem could be the wrong JOIN condition. Try LEFT JOIN instead of INNER JOIN.
More about joins: http://www.w3schools.com/sql/sql_join.asp
SELECT *
FROM `catalog_webdesign_products` t
LEFT JOIN tbl_member_registration t1
ON (t.userid = t1.fld_loginid AND t1.fld_member_category_level < 9)
WHERE t.product_img IS NOT NULL
GROUP BY t.userid
ORDER BY RAND() LIMIT 5