query to merge records based on value - php

I want to merge the value records as single row based on ID value.I want to display table1 value along with table2 value as single row. I want to display all in single record for the id=15.In table 1 ID is sub_id in table 2.
I want output as
Id content_value value as(15,cake,chocolate,enila,strabery)
select a.*,b.CONTENT_VALUE as multitype from album as a,album_details as b where a.ID=b.SUB_ID

Not possible to get as different columns for each value or content_value since the number of rows is dynamic.You can get the values like this by joining two tables. ie as two columns (ID and all content_value & value).
SELECT t1.ID, CONCAT_WS(',', t1.content_value, GROUP_CONCAT(t2.value)) AS contents FROM table1 t1
INNER JOIN table2 t2
ON t2.sub_id = t1.ID
GROUP BY t1.ID;

Related

PHP MySQL - Show Select Option as selected If Record exist in another table using JOIN Query

I was working on a task. In which
There is a select tag in which usersnames are showing from table1. I can choose multiple options.
& there is a submit button for adding those selected options in another table2 after choosing.
Working:
There are 2 Tables: table1 & table2
Data is coming from table1 in select option
I choose multiple options and click on submit.
those list of options foreign keys will be store in table2 from table1.
But when the page show again all the options that was selected previously should be shown now.
But I am afraid from the multiple queries that should be run. like currently, i am doing in this way.
I run the following query for showing data from table1 in select option
SELECT * FROM table1
and in the loop of php i am again checking that with a query that the option of this id from table1 is exist in table2 then show selected for that option. otherwise null.
My DB Tables
table1: id, name
table2: id
so if the id from table1 exist in table2 i shown selected else not show selected.
the only issue is i want to safe myself from running multiple queries within the loop and use some JOIN for this purpose. but i have no idea that how can we use if conditions in JOIN query.
Kindly help.
Thanks.
You want to use a LEFT OUTER JOIN
SELECT *
FROM table1 AS t1
LEFT OUTER JOIN table2 AS t2
ON t1.id = t2.id
WHERE t2.id IS NOT NULL
This basically says.
Return records where the table1.id = table2.id. Ignore records that do not exist in table2 based on id

get all record without join in sql

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

How to insert unique value in mysql

I am trying to upload data from cvs file to mysql. I want to filter the duplicate values of column roll.
for eg.
Table1
-----------
id name roll
1 Nirdos 4
2 krishn 2
3 shrest 2
If data is like this I want to insert first 2 rows because third row have duplicate row with second row.
Thnks In Advance
You can use the following INSERT INTO ... SELECT statement to do the insert:
INSERT INTO targetTable (id, name, roll)
SELECT t1.id, t1.name, t1.roll
FROM sourceTable AS t1
JOIN (SELECT roll, MIN(id) AS min_id
FROM sourceTable
GROUP BY roll) AS t2
ON t1.roll = t2.roll AND t1.id = t2.min_id
In case of a duplicate roll value, this query will insert the record having the minimum id value.
MySQL database provides a facility where you can make the roll column unique, and the database itself won't take duplicate input for that particular column.
The second method would be to check for duplicate entries before entering a new row and match the data with the data you are about to enter

Query for Selecting except values with two column

i have two tables.
First table column
value is
1,2,7.
Second table column
value is 1,2,3,4,5,6,7,8,9,10.
what i needed is i want to fetch second table values except first table values.Result should be 3,4,5,6,8,9,10.I do no what is the query for this one.Please help me.
SELECT value FROM secondtable WHERE value NOT IN (SELECT value FROM firsttable)
The standard SQL is to use NOT IN or NOT EXISTS:
select t2.*
from t2
where not exists (select 1 from table1 t1 where t1.value = t2.value);

Fetch all rows data from second table using single join query

I have run into a 1-N situation. i.e 1 record in 1st table will have multiple records in second table, I just wanna fetch all data corresponding to the ID in 1st table using joins, is this possible.
table 1 : ID name
1 pradeep
table 2: ID table1_id orders
1 1 23
2 1 25
3 1 26
In a single query i should get all records. I don't wanna loop as i am doing it and its taking lot of time.
I want to display user html output in manner like this. So there lies the problem.
ID Name orders1 orders2 orders order4
SELECT
*
FROM
table2
LEFT JOIN
table1 ON
table1.ID = table2.table1_id
SELECT
t2.* /* fetch data from second table */
FROM
table1 t1
LEFT JOIN table2 t2 on t2.table1_id = t1.id
WHERE t1.id = <id>;
I wouldn't recomment it though. Why don't you just run it as 2 separate queries?

Categories