Every derived table must have its own alias mysql - mysql

I have a temporary table named Days created as
CREATE TEMPORARY TABLE days (start_date DATETIME)as
SELECT DATE_ADD(begin_date, INTERVAL n - start_health_post_id DAY) as
start_date FROM NOF
ORDER BY n
LIMIT nofdays;
Now I want to have a cross join with a bunch of select statements that perform inner join
So, I tried this ways
Select * from days
cross join
(
Select practices.*, providers.*, practice_locations.*
from practices
Inner Join providers on practices.health_post_id = providers.health_post_id
Inner join practice_locations on practices.health_post_id =practice_locations.health_post_id
);
But It gives an error saying , every derived table must have its own alias.
please help!

Set an alias for your sub-query like:
Select * from days
cross join
(
Select practices.*, providers.*, practice_locations.*
from practices
Inner Join providers on practices.health_post_id = providers.health_post_id
Inner join practice_locations on practices.health_post_id =practice_locations.health_post_id
) t1;
I set t1 as alias.

Related

SQL Select two records if they have certain time difference of column A and have same column B value

I have 14000 records in my sql table. They have columns ID, test_subject_id and date_created. I want to fetch all the records that have been created within a time difference of 3 minutes(difference in date_created values) and both records should have the same test_subject_id.
You should use a self join, I assume inner join is what will work for you:
SELECT a.ID, a.date_created, b.ID, b.date_created
FROM accounts a
INNER JOIN accounts b
ON a.test_subject_id = b.test_subject_id
AND TIMESTAMPDIFF(MINUTE,a.date_created,b.date_created) = 3
Note: TIMESTAMPDIFF is used assuming date_created has type datetime, details here.
You can use EXISTS:
SELECT t1.*
FROM tablename t1
WHERE EXISTS (
SELECT 1
FROM tablename t2
WHERE t2.test_subject_id = t1.test_subject_id
AND ABS(TIMESTAMPDIFF(SECOND, t1.date_created, t2.date_created)) <= 180
)
ORDER BY t1.test_subject_id, t1.date_created;

select COUNT with three table - 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;

Give an Alias to a Joined Table MySQL

Is it possible to give an alias to a joined table in MySQL?
For example
Select *
From Customers
Natural Join Transaction as CT
Where (Select Count(CT.Customer_ID) ) >= 2
Would CT reference the joined table or the Transaction table?
And if it references the Transaction table how do I reference the joined table in a sub query?
No, you cannot do this in MySQL. In other databases, you could use a CTE for this purpose. (And in any database, you could use a temporary table, but that is not a good solution.)
Note: Do not use natural join. It is a bug waiting to happen.
To expression your query, use either an ON or USING clause. For your query, it would be something like this:
Select c.*, ct.*
From Customers c Join
Transaction as CT
ON c.Customer_ID = CT.Customer_ID JOIN
(select t2.Customer_ID, count(*) as cnt
from Transaction t
group by t2.CustomerId
where cnt >= 2
) cct
ON cct.Customer_ID = cct.Customer_Id;
Based on your comment:
Grab all customers who have 2 or more transactions
Here's one option using exists:
select *
from customers c
where exists (
select 1
from transaction t
where c.customerid = t.customerid
having count(*) > 1
)

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 query result issue with join

I have multiple tables as table_1 has id , p_code, profile_status, name and table_2 has id, p_code, availablity and table_3 has id, p_code, status...
How to get all records form all tables depend on p_code.
table_2 and table_3 has few records. if p_code not in table_2 and table_3 then echo 'no' in results.
currently i am using my query as below
select t.id, t.p_code,t.name,t.num_rooms, t.profile_status, t.distance FROM (
( SELECT id , p_code, profile_status, name,num_rooms, 3956 * 2 * ASIN(SQRT( POWER(SIN(($origLatAirport - latitude)*pi()/180/2),2)
+COS($origLatAirport*pi()/180 )*COS(latitude*pi()/180)
*POWER(SIN(($origLonAirport-longitude)*pi()/180/2),2)))
as distance FROM property WHERE profile_status=1 having distance < ".$dist." ) ) as t
How to add table_2 and table_3 and fetch results.
Pleasr reply soon. I am stuck here.
In your query you are doing CROSS JOIN and what you desire, is probably INNER JOIN.
In MySQL the CROSS JOIN behaves like JOIN and INNER JOIN of without using any condition.
The CROSS JOIN returns all rows form user multiplied by all rows from user_inbox - for every user you get inboxes of all users.
You should specify condition for your JOIN statement.
$sql_alt = mysql_query(
"select i.*,u.images, u.firstname, u.lastname
from user_inbox i INNER JOIN user u ON i.to_id = u.user_id
where i.to_id = '$user_id'");
Also it is good habit have the same names for primary and foreign keys, so I think you should have user_id or user_id_to instead of to_id in your user_inbox table. This is of course not absolutely necessary.