I have two separate tables on MySQL, and I want to make a query where I select * from both of them and I want to order them by id (or date). The problem is the id's will conflict, since there will be two articles with the same id (one from each table).
Is there a way where I can display all the articles of each table in one single query?
Edit:
They don't have the same number of columns. However, I will be choosing the same columns of each of them. So I'd be selecting "id, map, title, summary, image, date, publish" from both table1 and table2. (because both of the tables have those exact same columns).
Then I want to display on a single web page all the articles from both table1 and table2. In the exact same grid.
Is this even possible or do I need to merge both tables in MySQL?
If the 2 tables have the same number of columns and the data types of the corresponding columns are the same, then you can use UNION ALL:
SELECT * FROM table1
UNION ALL
SELECT * FROM table2
ORDER BY id
If you also want to distinguish the rows so you know from which table they originate, you can create a dummy 1st column for each of them like this:
SELECT '1' fromtable, * FROM table1
UNION ALL
SELECT '2' fromtable, * FROM table2
ORDER BY id
Related
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.
i have two tables table1 and table2, table1 in 10 filed available and tbale2 in 6 filed available.but no any relation between them.
i want to get all record from both table.
Use cross join
Select t.*,t1.* from table t cross join table1 t1
If you want all the records in the same table use the above query it will join and give m*n rows where m and n are number of rows in the tables
You can use union all if you want all the results added m+n number of results
Select * from table
Union all
Select * from table1
You need to specify the columns if you need specific columns from both tables. Or if you have different number of columns in the tables
If you have at least some common columns, you can union them together. For example:
Table1
Name Description Quantity Price
Table2
Name Description OrderDate Blah BlahBlah
You can do something like this:
SELECT Name, Description FROM Table1
UNION ALL
SELECT Name, Description FROM Table2
That would give you a result set with 2 columns (Name, Description, OrderDate) that is made up of rows from both Table1 and Table2
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%"
I have two tables. One tracks Part Shipments and the other tracks System shipments.
I am trying to count the customer contacts in each table with the result showing me the total customer contacts for both parts and systems combined.
I am trying to use Union and I would guess from my results I am doing this all wrong. My results end up with two entries for customers. Cust A will have a total of 9 and then another entry of 1. So I am guess there is no merge of the customer contacts and it is just creating a union of both results.
The Code I am using.
SELECT Count(part_shipment.Customer_Station_ID) AS Contact,
part_shipment.Customer_Station_ID AS Customer
FROM part_shipment
GROUP BY part_shipment.Customer_Station_ID
UNION
SELECT Count(system_shipments.Customer_Station_ID) AS Contact,
system_shipments.Customer_Station_ID AS Customer
FROM system_shipments
GROUP BY system_shipments.Customer_Station_ID
ORDER BY Contact DESC
You can't do it like that. The Union just take rows from first query and rows from second query, and "display" them ones after anothers.
UNION requires the creation of derived tables (tables created from a query).
SELECT *
FROM (
SELECT col1, col2
FROM table
) UNION (
SELECT col1, col2
FROM otherTable
)
I also don't think you can use GROUP BY inside the selects that make up the UNION (it's been a while since I used it so I don't remember for sure)
Do you have tried to use a GROUP BY and SUM from the results of UNION query?
I have a keyword search that searches multiple DB tables for thumbnail images using UNION ALL. I have two pages, results.php, and view.php. My goal is to able to click a thumbnail image on results.php and be directed to a larger version of that same image on view.php. The problem is each image in all my tables uses the field name "id" so when I click a thumbnail on results.php I get two different images with the same id from different tables. I tried changing the id's to different names, but when it was time to pass url parameters I can only choose 1 value. (if you can choose more than 1 I don't know how). So my question is why are my id's from different tables being grouped together, and how can I change this?
Image Results Page (which works perfect):
SELECT *
FROM table1
WHERE keyword LIKE %colname% OR id LIKE %colname%
UNION ALL
SELECT *
FROM table2
WHERE keyword LIKE %colname% OR id LIKE %colname%
View Image Page (having problems here):
SELECT *
FROM table1
WHERE id = colname
UNION ALL
FROM table2
WHERE id = colname
you need to namespace your table field names:
SELECT * FROM table1 WHERE table1.id = colname UNION ALL FROM table2 WHERE table2.id = colname
Add a derived field to both of the subqueries, so you can figure out which table that particular row came from:
SELECT 'table1' AS source, *
FROM table1
...
UNION ALL
SELECT 'table2' AS source, *
FROM table2
then have your code check that new 'source' field so you can point at the appropriate DB table later on when it comes time to display that image.
Of course, if both tables have an identical structure, that begs the question of why you didn't just combine them into a single table and add this 'source' field in this new "global" table, saving the trouble of having to run two queries to fetch image data.
Don't forget you can use the AS keyword to alias fields and tables
SELECT *
FROM table1 as t1
WHERE t1.keyword LIKE %colname% OR t1.id LIKE %colname%
UNION ALL
SELECT *
FROM table2 as t2
WHERE t2.keyword LIKE %colname% OR t2.id LIKE %colname%
SELECT *, 1 as table_reference
FROM table1
WHERE id = colname
UNION ALL
select *,2
FROM table2
WHERE id = colname
Your id's aren't being grouped together by mysql. It's just that you're requesting the database results as associative php arrays, with the column names as the array keys. You can't have duplicate array keys, so you only get one of the values.
If you don't need to know the name of the table, just fetch a numerically indexed array instead of associative array. If you need the table names, either modify your sql to alias the columns, or you can use more automated methods
SQL Select * from multiple tables
1) If all your tables have the same structure,
its a huge design flaw
and you ought to merge them in one table.
2) if your tables are different and you have to show only one image - what's the point in using union on the view page? if it's just one image from one table - why do you query 2 tables?