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;
Related
I have one join table:
table1_id|table2_id
1|1
1|2
2|1
2|2
3|1
I need to select rows grouped by table2. But I need special grouping. In result row I want to see the following:
table1.id|grouped
1,2|1,2
3|1
I tried different ways.
Select from table1 and group by table2.id. Fail - MySql required query to be grouped my primary table as well.
Select from table2 and group by table1.id and table2.id . Fail - it selects rows from table2 separately, I want them to be concatenated as I shown in the example.
Select from table1 and group by group_concat(table2.id). But MySql does not allow to group by aggregated functions.
My example query: http://sqlfiddle.com/#!9/934b64/4. I need almost the same, but so that it will one row with values:
1,2|1,2
3|1
You are almost there.
You need to use GROUP_CONCAT in SELECT clause, not in GROUP BY clause.
SELECT table1_id,GROUP_CONCAT(table2_id) as grouped
FROM table
GROUP BY table1_id;
See the result in SQL Fiddle
For your requirement, please try the below query:
select t1.id,t1.value,
group_concat(t2.id) as grouped_ids,
group_concat(t2.value) as grouped_values
from table1 t1
join joined_table jt on jt.id1 = t1.id
join table2 t2 on t2.id = jt.id2
group by t1.id,t1.value
Result in SQL Fiddle
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.
I need to join two tables in SQL. There are no common fields. But the one table have a field with the value krin1001 and I need it to be joined with the row in the other table where the value is 1001.
The idea behind the joining is i have multiple customers, but in the one table there customer id is 'krin1001' 'krin1002' and so on, in this table is how much they have sold. In the other table there customer is is '1001' '1002' and so on, and in this table is there name and adress and so on. So it will always be the first 4 charakters i need to strip from the field before matching and joining. It might not always be 'krin' i need it to work with 'khjo1001' also, and it still needs to join on the '1001' value from the other table.
Is that possible?
Hope you can help me.
You need to use substring:
ON SUBSTRING(TableA.Field, 5, 4) = TableB.Field
Or Right:
ON RIGHT(TableA.Field, 4) = TableB.Field
You can also try to use CHARINDEX function for join operation. If value from table1 contains value from table2 row will be included in result set.
;WITH table1 AS(
SELECT 'krin1001' AS val
UNION ALL
SELECT 'xxx'
UNION ALL
SELECT 'xyz123'
),
table2 AS(
SELECT '1001' AS val
UNION ALL
SELECT '12345'
UNION ALL
SELECT '123'
)
SELECT * FROM table1 AS t
JOIN table2 AS T2 ON CHARINDEX(T2.val, T.val) > 0
Use it as:
SELECT
*
FROM table t1
INNER JOIN table t2 ON RIGHT(t1.col1, 4) = t2.col1;
How can I select the contents of two columns that reside in different tables in a mysql database?
You would need to use either a JOIN or UNION/UNION ALL.
This will depend on wht you require.
Lets say you want all values from table 1 col a and table 2 col b in seperate rows
You can use
SELECT ColA
FROM TABLE1
UNION ALL
SELECT ColB
FROM TABLE2
All Distinct Values
SELECT ColA
FROM TABLE1
UNION
SELECT ColB
FROM TABLE2
And lets say that the you want to display them in the same row, they should have some key that links them
SELECT ColA, ColB
FROM TABLE1 t1 INNER JOIN
TABLE2 t2 ON t1.ID = t2.ID
It would also be good to note that there are different types of Sql Joins
Different SQL JOINs
JOIN: Return rows when there is at
least one match in both tables
LEFT JOIN: Return all rows from the
left table, even if there are no
matches in the right table
RIGHT JOIN: Return all rows from the
right table, even if there are no
matches in the left table
FULL JOIN: Return rows when there is
a match in one of the tables
Use a JOIN.
http://w3schools.com/SQL/sql_join.asp
SELECT fields
FROM table_a a
JOIN table_b b
ON (a.id = b.foo_id)
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.