Generating duplicate value mysql query - php

Edited--
Table1 name is ownership_profile
Table2 name is socity_unit
Colum in Table1: NAME and UNIT_ID
Colum in Table2: wings and unit_no
How to join in one table

if there are N rows in table socity_unit with the same socity_id (as sid in table ownership_profile) then you will see the cartesian product of socity_unit X ownership_profile.
Thus, for every row in ownership_profile you will get each matching row in socity_unit.
Now, because you have a '*' in the select statement you will get all columns from both tables. If the first table has many-many columns then there is an illusion that the rows are duplicated, till you scroll to the right, in order to see the columns from second table ...
Is this your case?

Related

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 to merge records based on value

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;

sum row content with matching columns

Yes yes, I know. The title is stupid and hard to understand.
Question:
There's a DB1 and a DB2.
DB1 has columns called id and price. id is a unique number for each item like 301 and so on. There's only one of each id in this DB.
DB2 contains a column named id and one named price. This DB has the duty of collecting data about each id ( item ) via a php script.
This means multiple rows with the same unique id.
a picture to demonstrate:
What i'm trying to do:
a script that querys an id ( matches the id with another table ) and sums up all the price columns with the same id.
I understand innerjoin
SELECT db1.id, db2.price
FROM db1
INNER JOIN db2
ON db1.id=db2.id;
but I want to sum all db2.price rows before showing. What can I do?
What you want is this:
(I guess you want to show db1.price too (or other fields from db1, otherwise there is no meaning have this join)
SELECT db1.id, db1.price, db2s.price -- rest of db1 fields here
FROM db1
INNER JOIN (select id, sum(price) price from db2 group by id) as db2s
ON db1.id=db2s.id;
SQLFIDDLE
I don't think you need a join for that. It should be enough to just do:
SELECT id, sum(price) as price_sum FROM db2 GROUP BY id
This should give you 2 columns:
id of the price
sum of all prices for given price ID

Searching multiple tables in MySQL database

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%"

Double query with WHERE IN, or join

We have two major mysql (innodb) tabels. Both containing millions of records.
Here is a example of our structure
-- table 1 --
primary_id
child_id
-
-- table 2 --
id
structure
contents
It's not like this, but for the question it's the same.
We need to fetch about 50.000 records from table 2, that are linked to primary id 2022.
What is the most quickly way to do this.
This is what we came up with:
1)
Do a select with a join on the two tables.
2)
Do a select of the ids in table 1 getting 200.000 records and then a select WHERE IN (all id's) and a filter on the structure there.
Any ideas?
i would go with a join. Select records from first table with the primary_id you need and join the second table on the ids

Categories