Can anyone help me on how could I join two tables without merging the result into single row? Please see below query:
SELECT *
FROM resorderdetails rd
INNER JOIN resinvalidorderdetails ri
ON rd.itemid=ri.srcitemid;
Let say for example I have 1 row in resorderdetails table with field itemid=1 and I have 1 row in resinvalidorderdetails table with field srcitemid=1.
If we will going to execute the above query, it will return a result of single row [merging the data of two tables]
What I want to do is to have two rows as a result. The first row is the record came from resorderdetails and the other row is the record came from resinvalidorderdetails without using UNION ALL or UNION.
How could I do it? Is it possible?
It's not possible, because you wannna have :
SELECT * FROM resorderdetails rd
and
select * from resinvalidorderdetails
There are only joins operations and UNION which you don't want to use, and the JOINS will put together or remove commons ids and UNION will do exactly what you want
Beside to display a query with 2+ tables you need something in common(even the union need same number of columns).
Related
I am trying to display a combination of two tables in which all rows will be there from first table and only 1 row from second table on some condition.
I was using left join suggest me some solutions
I think what you are looking for is UNION or UNION ALL.
It basically appends the results of two queries.
So your query would look somewhat like
select a, b, c from oneTable
UNION ALL
select x, y, z from otherTable where id = 23
The individual select can have where clauses and all kinds of stuff you know from SQL.
I have two tables called tc_revenue and tc_rates.
tc_revenue contains :- code, revenue, startDate, endDate
tc_rate contains :- code, tier, payout, startDate, endDate
Now I need to get records where code = 100 and records should be unique..
I have used this query
SELECT *
FROM task_code_rates
LEFT JOIN task_code_revenue ON task_code_revenue.code = task_code_rates.code
WHERE task_code_rates.code = 105;
But I am getting repeated records help me to find the correct solution.
eg:
in this example every record is repeated 2 time
Thanks
Use a group by for whatever field you need unique. For example, if you want one row per code, then:
SELECT * FROM task_code_rates LEFT JOIN task_code_revenue ON task_code_revenue.code = task_code_rates.code
where task_code_rates.code = 105
group by task_code_revenue.code, task_code_revenue.tier
If code admits duplicates in both tables and you perform join only using code, then you will get the cartessian product between all matching rows from one table and all matching rows from the other.
If you have 5 records with code 100 in first table and 2 records with code 100 in second table, you'll get 5 times 2 results, all combinations between matching rows from the left and the right.
Unless you have duplicates inside one (or both) tables, all 10 results will differ in colums coming either from one table, the other or both.
But if you were expecting to get two combined rows and three rows from first table with nulls for second table columns, this will not happen.
This is how joins work, and anyway, how should the database decide which rows to combine if it didn't generate all combinations and let you decide in where clause?
Maybe you need to add more criteria to the ON clause, such as also matching dates?
I'm fairly new to MySQL.
I'm trying to take a summarized data(float) from two columns in two different tables and create a third table with total of two columns and one row which is the sum total ...
here is what I got so far:
SELECT SUM(column_from_table1),
SUM(column_from_table2)
From table1, table2
It gives me back 2 columns with the wrong numbers(sum).
Where I'm getting it wrong?
If you need more data in order to help me please let me know.
Your problem is that when you select from table1, table2, an implicit join is made. Either use 2 queries, or use a subquery:
SELECT SUM(column_from_table1) AS SUM1,
(SELECT SUM(column_from_table2) FROM table2) AS SUM2
From table1
I have one problem to solve
I have for example 2 or more tables in db
i need to select random row, 1 per each table
How to do this?
There are many ways to select a random row from a table. The easiest (which works well on small tables) is:
select t.*
from table1 t
order by rand()
limit 1;
If all your tables have the same columns, you can combine the results from multiple such queries using union all. You could also combine the rows into one mega-row using cross join.
I have two tables.
I query like this:
SELECT * FROM (
Select requester_name,receiver_name from poem_authors_follow_requests as one
UNION
Select requester_name,receiver_name from poem_authors_friend_requests as two
) as u
where (LOWER(requester_name)=LOWER('user1') or LOWER(receiver_name)=LOWER('user1'))
I am using UNION because i want to get distinct values for each user if a user exists in the first table and in the second.
For example:
table1
nameofuser
peter
table2
nameofuser
peter
if peter is on either table i should get the name one time because it exists on both tables.
Still i get one row from first table and a second from table number two. What is wrong?
Any help appreciated.
There are two problems with your SQL:
(THis is not the question, but should be considered) by using WHERE over the UNION instead of the tables, you create a performance nightmare: MySQL will create a temporary table containing the UNION, then query it over the WHERE. Using a calculation on a field (LOWER(requester_name)) makes this even worse.
The reason you get two rows is, that UNION DISTINCT will only suppress real duplicates, so the tuple (someuser,peter) and the tuple (someotheruser, peter) will result in duplication.
Edit
To make (someuser, peter) a duplicate of (peter, someuser) you could use:
SELECT
IF(requester_name='peter', receiver_name, requester_name) AS otheruser
FROM
...
UNION
SELECT
IF(requester_name='peter', receiver_name, requester_name) AS otheruser
FROM
...
So you only select someuser which you already know : peter
You need the where clause on both selects:
select requester_name, receiver_name
from poem_authors_follow_requests
where LOWER(requester_name) = LOWER('user1') or LOWER(receiver_name) = LOWER('user1')
union
select requester_name, receiver_name
from poem_authors_friend_requests
where LOWER(requester_name) = LOWER('user1') or LOWER(receiver_name) = LOWER('user1')
The two queries are independent of each other, so you shouldn't try to connect them other than by union.
You can use UNION if you want to select rows one after the other from several tables or several sets of rows from a single table all as a single result set.
UNION is available as of MySQL 4.0. This section illustrates how to use it.
Suppose you have two tables that list prospective and actual customers, a third that lists vendors from whom you purchase supplies, and you want to create a single mailing list by merging names and addresses from all three tables. UNION provides a way to do this. Assume the three tables have the following contents:
http://w3webtutorial.blogspot.com/2013/11/union-in-mysql.html
You are doing the union before and then applying the where clause. So you would get a unique combination of "requester_name,receiver_name" and then the where clause would apply. Apply the where clause in each select...
Select requester_name,receiver_name from poem_authors_follow_requests
where (LOWER(requester_name)=LOWER('user1')
or LOWER(receiver_name)=LOWER('user1'))
UNION
Select requester_name,receiver_name from poem_authors_friend_requests
where (LOWER(requester_name)=LOWER('user1')
or LOWER(receiver_name)=LOWER('user1'))
In your where statement, reference the alias "u" for each field refence in your where statement.
So the beginning of your where statement would be like: where (LOWER(u.requester_name) = ...
This is simlar to the answer you can see in: WHERE statement after a UNION in SQL?
You should be able to use the INTERSECT keyword instead of doing a nested query on a UNION.
SELECT member_id, name FROM a
INTERSECT
SELECT member_id, name FROM b
can simply be rewritten to
SELECT a.member_id, a.name
FROM a INNER JOIN b
USING (member_id, name)
http://www.bitbybit.dk/carsten/blog/?p=71