How to select from multiple tables with join - mysql

I am trying to select all data from databases Startuptier1 and connections where the company id = $select. Both databases have the column 'companyid' with the corresponding row.
None of the information regarding joins that I have found so far online is working. I have also tried unions. I assume I am missing something very obvious?
$sql = "SELECT * FROM startuptier1
JOIN connections ON startuptier1.companyid = connections.companyid
WHERE companyid= '$select';";
I expect to get the data from both databases from what I've read so far but all I'm getting is an SQL Error.

You are getting an error probably because, companyid is present in both tables and mysql cannot decide on which column to fire where condition:
Try the modified query:
$sql = "SELECT * FROM startuptier1
JOIN connections ON
startuptier1.companyid = connections.companyid
WHERE startuptier1.companyid in (".$select.")";

Note: Your database account should have appropriate permission. You should be able to perform operations on every database that you are accessing via joins
EXAMPLE 1:
SELECT * FROM DB1.table1 t1 JOIN DB2.table2 t2 ON t2.column2 = t1.column1;
EXAMPLE 2:
SELECT db1.artist.name, db2.painting.title
FROM db1.artist INNER JOIN db2.painting
ON db1.artist.a_id = db2.painting.a_id;

Related

SELECT * FROM db1 WHERE db1.table.value = db2.table.value

I'm working with mySQL db and trying to display the correct data for the user. In order to do that I check if the data that I call from one backend is equal to username from another backend like so
SELECT * FROM db1 WHERE db1.table.value = db2.table.value
Names of databases are A and B.
SELECT *
FROM `A.onboardings`
, `B.loginsystem`
WHERE onboardings.sales_email = loginsystem.username
The problem is I get an error A.A.onboardings doesn't exists and A.B.loginsystem doesn't exist pls help :(
You must use this form - from A onboardings
You have to put the backticks in the right pace, or else mysql things your table is called A.onboardings
As seen bleow the needs to be around the database and the table name
And the use of aliases helps to keep even in big queries a good overview and yu have to write less
"SELECT * FROM `A`.`onboardings` a1,`B`.`loginsystem` b1 WHERE a1.sales_email = b1.username"
Try this one( Change the query according to your DB name, table, and matching column name)
SELECT * FROM mydatabase1.tblUsers INNER JOIN mydatabase2.tblUsers ON mydatabase1.tblUsers.UserID = mydatabase2.tblUsers.UserID
The problem is that
`A.onboardings`
is not the same as
A.onboardings
The first is a table reference where there table name has a period in it. The second is for the onboardings table in database A.
In addition, you should be using JOIN!!!
SELECT *
FROM A.onboardings o JOIN
B.loginsystem ls
ON o.sales_email = ls.username;
If you feel compelled to escape the identifies -- which I do not recommend -- then:
SELECT *
FROM `A`.`onboardings` o JOIN
`B`.`loginsystem` ls
ON o.sales_email = ls.username;

MySQL can only use 61 tables in a join; How can I bypass this error for the following query?

I get a MySQL Error saying, I cannot use more than 61 tables in a join. I need to avoid this error. How do I do it? Please Help.
select
view_pdg_institutes.user_id as User_ID,
view_pdg_institutes.institute_id as Teacher_ID,
view_pdg_institutes.institute_name as Institute_Name,
view_pdg_institutes.user_email as Email,
view_pdg_institutes.contact_person_name as Contact_Person,
view_pdg_institutes.alternative_contact_no as Alternative_Mobile_No,
view_pdg_institutes.primary_contact_no as Mobile_No,
view_pdg_institutes.correspondance_address as Address,
view_pdg_institutes.other_communication_mode as Preferred_Contact_Mode,
view_pdg_institutes.size_of_faculty as Size_of_Faculty,
view_pdg_institutes.operation_hours_from as Operation_Hours_From,
view_pdg_institutes.operation_hours_to as Operation_Hours_To,
view_pdg_institutes.teaching_xp as Teaching_Experience,
view_pdg_institutes.installment_allowed as Installment_Allowed,
view_pdg_institutes.about_fees_structure as About_Fees_Structure,
view_pdg_institutes.no_of_demo_class as No_of_Demo_Classes,
view_pdg_institutes.demo_allowed as Demo_Allowed,
view_pdg_institutes.price_per_demo_class as Price_Per_Demo_Class,
view_pdg_tuition_batch.tuition_batch_id as Batch_ID,
view_pdg_batch_subject.subject_name as Subject_Name,
view_pdg_batch_subject.subject_type as Subject_Type,
view_pdg_batch_subject.academic_board as Academic_Board,
view_pdg_batch_fees.fees_type as Fees_Type,
view_pdg_batch_fees.fees_amount as Fees_Amount,
view_pdg_tuition_batch.course_days as Course_Days,
view_pdg_tuition_batch.days_per_week as Days_Per_Week,
view_pdg_tuition_batch.class_duration as Class_Duration,
view_pdg_tuition_batch.class_type as Class_Type,
view_pdg_tuition_batch.course_length as Course_Length,
view_pdg_tuition_batch.course_length_type as Course_Length_Type,
view_pdg_tuition_batch.no_of_locations as No_of_Locations,
view_pdg_tuition_batch.class_capacity_id as Class_Capacity_ID,
view_pdg_tutor_location.locality as Locality,
view_pdg_tutor_location.address as Address,
view_pdg_batch_class_timing.class_timing as Class_Timing
from view_pdg_tuition_batch
left join view_pdg_institutes on (view_pdg_tuition_batch.tutor_institute_user_id = view_pdg_institutes.user_id)
left join view_pdg_batch_subject on (view_pdg_batch_subject.tuition_batch_id = view_pdg_tuition_batch.tuition_batch_id)
left join view_pdg_batch_fees on (view_pdg_batch_fees.tuition_batch_id = view_pdg_tuition_batch.tuition_batch_id)
left join view_pdg_batch_class_timing on (view_pdg_batch_class_timing.tuition_batch_id = view_pdg_tuition_batch.tuition_batch_id)
left join view_pdg_tutor_location on (view_pdg_tutor_location.tuition_batch_id = view_pdg_tuition_batch.tuition_batch_id)
group by view_pdg_tuition_batch.tuition_batch_id;
I need a solution that would not require changing the current approach of writing the query.
I don't think it's possible to do what you're asking without some elaborate changes in the way you store and query data. You can
denormalize your DB to store JSON data;
create materialized views, emulating them via triggers, because they're absent in MySQL;
use temporary tables;
join partial selects by hand at the call site;
compile MySQL with another join limit;
use proper SQL engine like Postgres, that doesn't suffer from such stupid things.
Insert the contents of each view into its own temporary table. Then do the same query with the temporary table names substituted for the original view names.

Optimizing MySQL query with nested select statements?

I've got read-only access to a MySQL database, and I need to loop through the following query about 9000 times, each time with a different $content_path_id. I'm calling this from within a PERL script that's pulling the '$content_path_id's from a file.
SELECT an.uuid FROM alf_node an WHERE an.id IN
(SELECT anp.node_id FROM alf_node_properties anp WHERE anp.long_value IN
(SELECT acd.id FROM alf_content_data acd WHERE acd.content_url_id = $content_path_id));
Written this way, it's taking forever to do each query (approximately 1 minute each). I'd really rather not wait 9000+ minutes for this to complete if I don't have to. Is there some way to speed up this query? Maybe via a join? My current SQL skills are embarrassingly rusty...
This is an equivalent query using joins. It depends what indexes are defined on the tables how this will perform.
If your Perl interface has the notion of prepared statements, you may be able to save some time by preparing once and executing with 9000 different binds.
You could also possibly save time by building one query with a big acd.content_url_id In ($content_path_id1, $content_path_id2, ...) clause
Select
an.uuid
From
alf_node an
Inner Join
alf_node_properties anp
On an.id = anp.node_id
Inner Join
alf_content_data acd
On anp.long_value = acd.id
Where
acd.content_url_id = $content_path_id
Try this extension to Laurence's solution which replaces the long list of OR's with an additional JOIN:
Select
an.uuid
From alf_node an
Join alf_node_properties anp
On an.id = anp.node_id
Join alf_content_data acd
On anp.long_value = acd.id
Join (
select "id1" as content_path_id union all
select "id2" as content_path_id union all
/* you get the idea */
select "idN" as content_path_id
) criteria
On acd.content_url_id = criteria.content_path_id
I have used SQL Server syntax above but you should be able to translate it readily.

Performing multiple joins while using IS NOT NULL in MySQL

I have several tables that I am selecting data from. I am trying to get a list of information out of these tables only where the fields
"interested_parties.emp_id" and "solicitation_entrys.sol_ent_id" are satisfied.
This is my query that I have come up with so far:
SELECT * FROM users
JOIN interested_parties ON (users.emp_id = interested_parties.emp_id)
JOIN department_colors ON (department_colors.dept_id = users.emp_dept)
JOIN solicitation_entrys ON (solicitation_entrys.sol_ent_id = interested_parties.sol_ent_id)
WHERE IS NOT NULL (interested_parties.emp_id, solicitation_entrys.sol_ent_id)
The goal is to select everything from these tables, where these fields are not null.
I'm getting #1064 errors from MySQL and I can't seem to solve the issue. Do I have to perform the if at the beginning of the select? If so, how would you do that with joins?
Thank you for your help.
Use this instead
SELECT * FROM users
JOIN interested_parties ON (users.emp_id = interested_parties.emp_id)
JOIN department_colors ON (department_colors.dept_id = users.emp_dept)
JOIN solicitation_entrys ON (solicitation_entrys.sol_ent_id = interested_parties.sol_ent_id)
WHERE interested_parties.emp_id IS NOT NULL
AND solicitation_entrys.sol_ent_id IS NOT NULL
You have to compare each field for not being null.
As MySQL should telling you, the error is in your SQL syntax.
The WHERE clause should be:
WHERE (interested_parties.emp_id IS NOT NULL)
AND (solicitation_entrys.sol_ent_id IS NOT NULL)

MySQL query question

I have a database containing the route data for airlines.
Some of the attributes are sources_airport_ID, destination_airport_ID, and airline(which supports the route from source to destination).
I'm trying to self join the table to look up airlines that have the same route (in other words, same sources_airport_ID and destination_airport_ID).
I was using the query as follows:
(table name = routes)
SELECT t1.*, t2.*
FROM routes AS t1, routes AS t2
WHERE t1.sources_airport_ID = t2.sources_airport_ID
AND t1.destination_airport_ID = t2.destination_airport_ID
AND t1.airline != t2.airline
When I execute this query, I get an error saying that the maximum execution time exceeded 300 seconds. I'm not sure if I'm using the correct query for this purpose.
Can anyone help me with the query? I'm using xampp as my database.
Thanks in advance!
EDIT:
My primary key is ID, which is just an auto-increment value.
There are 64,114 records in routes table.
Try using JOIN syntax:
SELECT t1.*, t2.*
FROM routes AS t1
JOIN routes AS t2
ON t1.sources_airport_ID = t2.sources_airport_ID
AND t1.destination_airport_ID = t2.destination_airport_ID
AND t1.airline != t2.airline
But as suggested do make sure that the fields sources_airport_ID, destination_airport_ID and airline are indexed in the routes table.
Try something like this:
SELECT r.*, count(r.sources_airport_ID) as occ
FROM routes r
GROUP BY sources_airport_ID, destination_airport_ID, airline
HAVING occ > 1;