mysql select multiple tables and bring back all results - php

I am trying to select everything from multiple tables where there are 2 conditions.
But they only bring back a single result from the second table, instead of everything
Here is the MySql code
SELECT *
FROM `core_users`.`users`
LEFT JOIN `core`.`orders`
ON `core_users`.`users`.`uid` = `core`.`orders`.`uid`
LEFT JOIN `core_users`.`crm`
ON `core_users`.`users`.`uid` = `core_users`.`crm`.`uid`
WHERE (`core_users`.`users`.`signup_timestamp` BETWEEN '1476626400' AND '1476712800'
OR (`core_users`.`crm`.`type` = 'refresh_5in5' AND `core_users`.`crm`.`value` BETWEEN '1476626400' AND '1476712800') )
This just brings back 1 result from the crm table. However I want it to bring back all the results from the crm table.
How do I bring back everything from users, orders, and crm while having a WHERE clause on both tables?

Try select users.*, orders.*, crm.*
Please note that, in such case if you have common columns in any of these table like id in every table it would cause an ambiguous column name error. To get rid of that you need to specify these with alias name users.id as user_id, orders.id as order_id, crm.id as crm_id and so on.

Related

SQL Query Phpmyadmin is not giving correct results having more than 3 tables with WHERE Clause

I am having relational database and trying to execute following query up to 3 tables It is giving me correct results i.e. 248 records of students in institute id 910, but when I try to make a query having more than 3 tables it gives me 19000+ results.
SELECT *
FROM Student,StudentRegistration,RefStudentType,RefGender,SubjectCategory
WHERE Student.student_id=StudentRegistration.student_student_id
AND StudentRegistration.reg_student_type_std_type_id = RefStudentType.std_type_id
AND Student.student_gender_gender_id = RefGender.gender_id
AND StudentRegistration.reg_student_subjectCat_sub_cat_id=SubjectCategory.sub_cat_id
AND Student.student_institute_inst_id=910;
`
Tried with JOIN as well but same 19000+ records incorrect results
SELECT * FROM Student INNER JOIN StudentRegistration ON student_id=student_student_id INNER JOIN RefReligion ON RefReligion.religion_id=Student.student_religion_religion_id INNER JOIN RefStudentType ON RefStudentType.std_type_id=StudentRegistration.reg_student_type_std_type_id WHERE student_institute_inst_id=910;
Any solution, query logical errors or something new
I think this is due to having several records for one data. For example, there might be several records in the SubjectCategory table for id = '910'
It is best to use left/right/inner/outer joins without using from tbl1, tbl2
What I suggest is to check the tables one by one with the id.

Displaying INNER JOIN results as HTML table on webpage?

I have the following query:
$sql = "SELECT first, COUNT(FIRST) FROM techs INNER JOIN ros ON techs.id = ros.writtenby GROUP BY ID";
That gives the desired output of:
first
COUNT(FIRST)
Eric
88
John
11
I have no earthly idea how to display this EXACT table onto a PHP/HTML page. I've created tables based off of queries but it's basically just displaying database table data. I have a feeling the INNER JOIN is the root of my issue since it's not an actual table in the database, rather just results from my query. If this isn't apparent enough already I'm very much a beginner at coding in general so be kind. Lol.
From comments:
I don't understand how to call and then display the "count(first)" column of my query results, since "`count(first)" isn't an actual column in either of the tables I am generating the result from.
If only that's your problem, use alias, like:
SELECT first, COUNT(FIRST) AS my_count FROM techs INNER JOIN ros ON techs.id = ros.writtenby GROUP BY ID
Note that above is the query you posted, just with " AS my_count" added.

Multi-Table, Multi-WHERE and SELECT MySQL query

So, I am trying to select some data from 4 tables using a query I have attempted to throw together.
SELECT *
FROM cards
LEFT JOIN cards_viewers ON cards.card_id = cards_viewers.card_id
(SELECT *
FROM folders
WHERE folder_id = cards.card_folderID)
(SELECT user_firstName,
user_lastName,
user_avatar
FROM user_data
WHERE user_id = cards_viewers.user_id)
WHERE cards_viewers.user_id = '.$u_id.'
ORDER BY cards.card_lastUpdated DESC
Basically, the query selects data from the four tables depending on the user_id in table user_data. I have attempted to initially fetch all data from the tables cards, and cards_viewers, and have went on to use this data to select values from the other tables (user_data and folders).
The query is wrong, I know that. I have learnt the majority of basic MySQL, but I am still struggling with more complex queries like the one I am trying to write now. What query can I use to select the data I want?
Links to any documentation to parts of queries would prove very useful in helping me learn how to create queries in future, rather than just relying on StackOverflow.
Many thanks.
You don't need "MULTI-WHERE" but multiple joins, you just need to keep doing joins until you get the tables you need.
Here's an example:
SELECT *
FROM cards LEFT JOIN cards_viewers
ON cards.card_id = cards_viewers.card_id
LEFT JOIN folders
ON folders.folder_id = cards.card_folderID
LEFT JOIN user_data
ON user_id = cards_viewers.user_id
WHERE cards_viewers.user_id = '.$u_id.'
ORDER BY cards.card_lastUpdated DESC
To custom the fields you want to get just change * for the name of the field being careful about ambiguous column naming.
For further information check MySql Joins. Hope this helped you :)

MySql single query, two tables same field name

I have two database tables one called clients, the other one users. I'm trying to select single field "email" from both tables. Note, that email field is in both tables.
Table clients contains 10 records, while users has 5 records.
The below query works, but for some reason records are repetitive, so instead of getting 15 records in total, I end up with 50. Seems like records from one table are multiplied by records from other table.
SELECT
clients.email,
users.email
FROM
clients,
users;
Any help would be appreciated.
Your query returns cartesian product
You can simply use union all
select email from clients
union all
select email from users
You are getting 50 rows because cross join is happening there, go for inner join or left join or right join as per your choice, most preferably inner join...
Take some common attributes in both like id or something...
select clients.email, users.email from clients,users where clients.id=users.id...
try renaming the fields
For Clients table
clients_email
For Users table
users_email
then query as
SELECT clients.clients_email, users.users_email FROM clients, users WHERE clients.clients_email = users.users_email;
Hope it helps

Getting SQL result as nested array in PHP

I've a ('courses') table that has a HABTM relationship with ('instructors') table through another table...
I want to get the data of an instructor with all related courses in one query..
Currently, I have the following SQL:
SELECT *
FROM `instructors` AS `instructor`
LEFT JOIN `courses` AS `course`
ON `course`.`id` IN (
SELECT `course_id`
FROM `course_instructors`
WHERE `course_instructors`.`instructor_id` = `instructor`.`id`
)
WHERE `instructor`.`id` = 1
This SQL does what it should be doing, the only "problem" I have is that I get multiple rows for each joined rows.
My question is:
Can I get the result I want in one query? Or do I have to manipulate the data in PHP?
I'm using PHP and MySQL.
Each record of a query result set has the same format: same number of fields, same fields, same order of fields. You cannot change that.
SELECT *
FROM instructors AS instructor
LEFT JOIN
course_instructors
ON
instructor.id= course_instructors.instructor_id
LEFT JOIN
courses
ON
course_instructors.course_id = course.id
WHERE instructor.id = 1
This assumes the PK of course_instructors is (instructor_id,course_id)
Explanation of query:
First join + WHERE make sure you get the relevant instructor
Second join matches ALL the entries from the course_instructor table that belongs to this instructor. If none found, will return one row with NULL in all fields
Last join matches all relevant courses from the entries found from course_instructor If none would will return one record with NULL in all fields.
Again: important to use the right constraints to avoid duplicate data.
That's the nature of relational databases. You need to get the instructor first and then get the related courses. That's how I would do it and that's how I've been doing it. I'm not sure if there is a "hack" to it.

Categories