Selecting from two tables, with different columns, where one needs a count - mysql

I have two tables, TableA and TableB.
I need to select one count value from TableA, based on a where condition.
I need to select two values from TableB.
I'd like all the values in one result set. There will never be more than one row in the result set.
Here's what I have now:
SELECT count(id) FROM TableA WHERE ($some_where_statement) SELECT owner, owner_ID from TableB
I know this should be simple, but this is throwing an error. Any suggestions?

You can cross join to join rows from two unrelated tables:
SELECT T1.cnt, T2.owner, T2.owner_ID
FROM (SELECT count(id) FROM TableA WHERE ($some_where_statement)) AS T1
CROSS JOIN (SELECT owner, owner_ID from TableB) AS T2
To have only one row in the result set, it is assumed that both subqueries only return one row. I suspect that this is not the case for the second subquery. You are probably missing a where clause.

Related

Select Count from Two Tables = Multiplication in SQL?

I randomly tried running a query like:
select count(*) from table1, table2
The result was essentially multiplication of the actual row count of the two tables, i.e. the result was 645792 rows based on the fact that table1 had 868 rows, and table2 had 744 rows.
Is this an expected behaviour, I checked out the documentation but could not get any better understanding of this behaviour.
This is your from clause:
from table1, table2
This is equivalent to:
from table1 cross join table2
This is a cartesian product of both tables, which generates a resultset containing 868 * 744 rows. Then count(*) just counts the number of resulting rows, hence the result that you are getting.
If you wanted to sum the number of rows in each table, you would compute two separate counts:
select
(select count(*) from table1)
+ (select count(*) from table2) total_no_rows
Your current query:
select count(*) from table1, table2
is using the old school implicit join syntax. As there is no join criteria appearing in a WHERE clause (there is no WHERE clause), the join defaults to being a cross join. This is just the cross product between records in the two tables, which is what you are currently seeing. A better way to write your query would be:
SELECT COUNT(*)
FROM table1
CROSS JOIN table2;
'INNER JOIN and , (comma) are semantically equivalent in the absence of a join condition: both produce a Cartesian product' https://dev.mysql.com/doc/refman/8.0/en/join.html

Performance compare, get concat string by subquery or join table?

I have a query need to get more then one field from another table, which one of the following will be faster?
1
SELECT *, (SELECT CONCAT(fieldA,'|',fieldB) FROM tableB WHERE bid=id) as moreField FROM tableA
2
SELECT tableA.*, tableB.fieldA, tableB.fieldB FROM tableA INNER JOIN tableB ON bid=id
Actually, I need fieldA and fieldB as separated values, so in case one I will use explode('|',moreField) in PHP to get them separated after the query. Just want to know which query will be faster.
In my experience, nested queries are slow.
If you need concatened results You could do :
SELECT tableA.*, tableB.fieldA, tableB.fieldB, CONCAT(tableB.fieldA,'|',tableB.fieldB) as moreField
FROM tableA INNER JOIN tableB ON bid=id
But you say that you actually need separated values so just use your second solution.

Regarding select query in the column

Want to have the multiple columns returned from select query in the column of the main select query. Getting MySQL error. How can I do it?
SELECT test.val1,
(SELECT
cc.id, cc.nbr,cc.addr
FROM
(
Select temptable.id ,temptable.nbr,temptable.addr
from temptable
inner join temptable2 on temptable2.id=temptable.id
) cc),
test.val2,
test.val3
FROM test
innerjoin test2 on test.id=test2.id
A subquery in the select list has to return 1 field and 1 record. If you want values from multiple fields to appear in a single column, then you can use concat() or concat_ws() mysql functions to concatenate them into a single field. If you want to combine multiple fields and multiple rows into one field, then above the afore mentioned 2 functions you will need group_concat().
select concat_ws(';',field1, field2) from table
However, I think in this particular case you may want to place the subquery into the from clause and reference the columns from there in the select clause.
Please try the below if the four tables are related.
select tb1.tb_id, tb1.val1, tb2.tb_id from (select test.id as tb_id, val1, val2, val3 from test inner join test2 on test.id=test2.id) as tb1 join (select temptable.id as tb_id from temptable join temptable2 on temptable.id=temptable2.id) as tb2 on tb2.tb_id=tb1.tb_id;

Get number of duplicate rows resulting from a DISTINCT query

I have a table with rows where a, b, and c are commonly the same.
I have a query that gives me each unique record. I'm trying to get the count, of the duplicate records for each distinct record returned.
SELECT DISTINCT
a,
b,
c,
COUNT(id) as counted
FROM
table
The COUNT here returns the count for all the records. What I was looking for was the count of records identical to the unique record.
SELECT a,b,c,COUNT(*) FROM table GROUP BY a,b,c
SELECT DISTINCT
a,
b,
c,
(
SELECT
COUNT(id)
FROM
table_name t1
WHERE
t2.a = t1.a
) AS counted
FROM
table_name t2
The above sub query know as inline sub query. in where clause t1 and t2 treat as different table(It's single table in DB) by query. So it check the equality and then count. as we put distinct for a column so all play done with that only.
I hope am able to enplane.
Ah, figured this one out from a duplicate as I was writing the question - I figured I'd share my results as they were different enough from the answer I got mine from.
I have to use a subquery to get query non-distinct records. Then, I can use results from the first query in the subquery's WHERE clause.
SELECT DISTINCT
a,
b,
c,
(
SELECT
COUNT(id)
FROM
table_name t1
WHERE
t2.a = t1.a
) AS counted
FROM
table_name t2
This works. Let me know if there are gaps in my understanding.
With help from this answer: https://stackoverflow.com/a/14110336/1270996

SQL outer join which omits common records

I'm trying to formulate an SQL FULL OUTER JOIN, which includes all values in table A and table B, but not those values common between them.
I have searched the internet, and stumbled upon the following SQL code:
SELECT * FROM TableA
FULL OUTER JOIN TableB
ON TableA.name = TableB.name
WHERE TableA.id IS null
OR TableB.id IS null
Which can be illustrated like so:
I'm not sure I understand the IS null parts. Could the SQL be carried out by simply stating something like the following as a WHERE condition? :
WHERE TableA.id <> TableB.id
What is it you don't understand about the IS NULL clauses?
In an OUTER JOIN (LEFT, RIGHT, FULL) there's a chance that columns from the outer table could end up as NULL.
The clauses
WHERE TableA.id IS null
OR TableB.id IS null
are simply saying that one of the IDs has to be NULL, I.E. if you have a row from TableA there can't exist a matching row from TableB and vice versa.
SELECT Name, ID FROM TableA UNION SELECT Name, ID FROM TableB
EXCEPT
SELECT Name, ID FROM TableB INTERSECT SELECT Name, ID FROM TableA
The first select gets all rows from table A and table B and combines this into 1 result set.
The second select selects all rows that are common between the two.
What the except does is select all rows from the first select - all rows from the second select.
What you end up with is all rows - the rows that are common between the two tables.