Get CSV from query which involves multiple tables - php

I'm trying to make a query to extract elements from 2 tables, which are linked via another table.
So I have 3 tables:
authors
- id, name, book
category
- id, name, description
category-author
- id, idauthor, idcategory
Now I want to make a query to make the following output:
row: authors.id, authors.name, authors.book, category.name
I don't know what category's are linked using the 2 tables only, so I need to use the last one, the category-author table. The id's of both the author and the category are linked via that table.
I got the following query:
SELECT authors.id, authors.name, authors.book, category.name FROM category, author LEFT JOIN SELECT ??
I'm stuck at the remaining part of the query.
Also when I have this query, can I just extract a CSV with phpmyadmin?

You can get related information from different tables using table joins. Relations between tables should be specified using foreign keys (i.e. the column idcategory from category-author is presumably a foreign key that refers to primary key column category.id). In a join clause, you merely specify which tables are to be joined and on what column:
SELECT table1.col1, table2.col2
FROM table1
JOIN table2 ON table1.pkCol = table2.fkCol
This means you can't specify any SELECT or FROM clauses within a JOIN clause. The columns you wish to select from joined tables are all specified in the initial SELECT statement, and you only specify one table in the FROM clause, from which you subsequently perform the table joins.
In your case, I think this should get you started:
SELECT authors.id, authors.name, authors.book, category.name
FROM category
LEFT JOIN category-author ON category-author.idcategory = category.id
LEFT JOIN authors ON authors.id = category-author.idauthor
I'm not sure how familiar you are with foreign keys, primary keys and table joins, so I won't elaborate any more on this. I think specifying multiple tables in a FROM clause is bad practice, even if your database system still supports it (Related question).
From then on, you can easily export the results from within PhpMyAdmin, as there is an export button for every table overview, including query results.

Related

How do you join two tables together without duplicating results?

I have two tables in a SQL database in which one references the other. One table is cpddata and the other is subdb. Each entry in cpddata may have between 0 and 50 subdb entries that reference the id from cpddata, for example, if I create an entry in cpddata and it's id number is 30, I have a column in subdb that is named cpdid and each subdb entry that relates to the cpddata entry will be assigned the 30 value.
In the past when I have had to reference one table to another table I have used SQL joins but if I join cpddata and subdb on cpdid then I get a duplicated result output for every subdb that is associated with the cpddata entry.
What I would like to be able to accomplish is to return the single cpddata entry but still have the subdb data attached to the correct cpddata entry and be filterable with AngularJS. Is there a "correct" method to achieving this?
USE DISTINCT
SELECT distinct C.ID, S.ID
FROM cpddata C
INNER JOIN subdb S
ON C.ID = S.cpdid

Searching multiple tables in MySQL database

I have several different tables in my database(mySQL).
Here are the relevant coumns for the tables
table_tournaments
tournamentId
tournamnetName
tournamentStatus
table_tournament_results
tournamentId
playerId
playerName
playerRank
tournamnetParticipants
table_players
playerId
playerName
The tournaments table contains the information about the tournament, the tournament results table shows the results from that table
I want to search the tournaments table by name and then with the returned results get the information from the tournament results table.
SELECT * FROM `tournaments` `WHERE` `tournamentName` LIKE "%Query%"
I'm not sure how to go about this, maybe I need to do something via PHP, any and all help is appreciated.
You can get the results you want with a join operation.
This is an example of an outer join, returning all rows from t that have the string 'foo' appearing as part of tournament_name, along with any matching rows from r.
A relationship between rows in the two tables is established by storing a common value in the tournamentId column of the two tables. The predicate in the ON clause specifies the condition that determines if a row "matches".
SELECT t.tournamentId
, t.tournamentName
, t.tournamentStatus
, r.playerId
, r.playerName
, r.playerRank
FROM table_tournaments t
LEFT
JOIN table_tournament_results r
ON r.tournamentId = t.tournamentId
WHERE t.tournament_name LIKE '%foo%'
ORDER
BY t.tournamentId
, r.playerId
The t and r that appear after the table names are table aliases, we can qualify references to the columns in each table by prefacing the column name with the table alias and a dot. This makes the column reference unambiguous. (In the case of tournamentId, MySQL doesn't know if you are referring to the column in t or r, so we qualify it to make it explicit. We follow this same pattern for all column references. Then, someone reading the statement doesn't need to wonder which table contains the column playerId.
Your Query may be like this
SELECT a.*, b.tournamnetName FROM table_tournament_results a
left join table_tournaments on a.tournamentId=b.tournamentId
WHERE b.tournamnetName LIKE "%Query%"

php and mysql queries

I'm creating a website where the user can add some information about multiple computers into a database.
In the mysql database I have 3 tables. TypeOfMachine, Software and Hardware. The only common thing they have is the NumberOfMachine.
I must create a page where the user can run reports showing devices that have a specific piece of software installed (user specified) or specific hardware (user specified) after he submitted the computer's info.
Any ideas how I can do this?
And how I can connect all 3 tables into one?
I thought about this code but i dont know what to put in WHERE. I have 10 variables. and I have to show all the computers with what the user has asked and their info as well!
$search1 = "
SELECT
TypeOfMachine.NumberOfMachine, TypeOfMachine.TypeOfMachine, TypeOfMachine.OS, Software.Software1, Software.Software2, Software.Software3, Hardware.SSD, Hardware.Harddisk, Hardware.MonitorSize, Hardware.Ram, Hardware.Rom, Hardware.Processor
FROM TypeOfMachine, Software, Hardware
WHERE
but i
You want to use a join. This example is based on the fact that you've said the NumberOfMachine field is present in all tables and is a common link between them:
SELECT
TypeOfMachine.NumberOfMachine,
TypeOfMachine.TypeOfMachine,
TypeOfMachine.OS,
Software.Software1,
Software.Software2,
Software.Software3,
Hardware.SSD,
Hardware.Harddisk,
Hardware.MonitorSize,
Hardware.Ram,
Hardware.Rom,
Hardware.Processor
FROM TypeOfMachine
LEFT JOIN Software
ON Software.NumberOfMachine = TypeOfMachine.NumberOfMachine
LEFT JOIN Hardware
ON Hardware.NumberOfMachine = TypeOfMachine.NumberOfMachine
WHERE
...
It's general question, I don't know which tables contains a spesific columns as indicator for all tables. It's about inner and outer join:
The two common types of joins are an inner join and an outer join. The difference between an inner and outer join is in the number of rows included in the results table.
Inner join: The results table produced by an inner join contains only rows that existed in both tables.
Outer join: The combined table produced by an outer join contains all rows that existed in one table with blanks in the columns for the rows that did not exist in the second table.
For instance, if table1 contains a row for Joe and a row for Sally, and table2 contains only a row for Sally, an inner join would contain only one row: the row for Sally. However, an outer join would contain two rows — a row for Joe and a row for Sally — even though the row for Joe would have a blank field for weight.
The results table for the outer join contains all the rows for one table. If any of the rows for that table don’t exist in the second table, the columns for the second table are empty. Clearly, the contents of the results table are determined by which table contributes all its rows, requiring the second table to match it.
Two kinds of outer joins control which table sets the rows and which must match: a LEFT JOIN and a RIGHT JOIN.
You use different SELECT queries for an inner join and the two types of outer joins. The following query is an inner join:
SELECT columnnamelist FROM table1,table2
WHERE table1.col2 = table2.col2
And these queries are outer joins:
SELECT columnnamelist FROM table1 LEFT JOIN table2
ON table1.col1=table2.col2
SELECT columnnamelist FROM table1 RIGHT JOIN table2
ON table1.col1=table2.col2
In all three queries, table1 and table2 are the tables to be joined. You can join more than two tables. In both queries, col1 and col2 are the names of the columns being matched to join the tables. The tables are matched based on the data in these columns. These two columns can have the same name or different names, but they must contain the same type of data.
For general concept you can use #Scrowler suggestion, or this one:
http://stackoverflow.com/questions/1204217/mysql-select-join-3-tables

Msql Inner Join Query Dilemma - Need Joined Auto Increment Value

I have two mysql tables that I am querying together and the query works fine. The tables are car and requests
The problem is that i need both the auto increment id's and it only gives me one.
The one it gives me is not the joined tabled.
Here is my query so far
SELECT * FROM `car` INNER JOIN `requests` ON `car`.`make_id` = `requests`.`make_id` WHERE `car`.`user_id` =21
I just need to someway get the auto increment id of the requests table.
As always stack exchange is the best place to come for answers so thanks in advance!
Just specify your query as this and you'll get both but with different names:
SELECT `car`.id AS car_id, `requests`.id AS request_id, *
FROM `car`
INNER JOIN `requests` ON `car`.`make_id` = `requests`.`make_id`
WHERE `car`.`user_id` =21
Note that you will still have an ID column which SHOULD be the requests.id, just diregard it and use car_id and request_id...
When you have to join tables and table contains same name than it creates an ambiguous situation. So always use table name or table alias with column name Like this
SELECT t.column
FROM table as t
OR
SELECT table.column
FROM table

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