I have two mysql tables:
table 1: product
productId productName
1 One
2 Two
table 2: branch
branchId branchName
1 Branch One
2 Branch Two
3 Branch Three
these two tables need to be merged into a third table
table 3: productopening
openId productId branchId
The id is an auto increment id. the first two tables have data that are not related. I mean, row for id=1 in table 1 has nothing to do with the row for id=1 in table two. So, I basically want to write a mysql script which would insert values into table 3 to look like this in the end:
table 3: productopening
openId productId branchId
1 1 1
2 1 2
3 1 3
4 2 1
5 2 2
6 2 3
the ids in the old tables and the new table don't have to match. Just the data from the tables need to be in the new table. I am very new to mysql and if anyone can help me with this, it would be great!
That's It.
I hope this query will work:
INSERT INTO productopening(SELECT null, product.productId, branch.branchId FROM
product CROSS JOIN branch ORDER BY product.productId);
You can use cross join and insert select
insert into productopening
select product.id, branch.id
from product
crossjoin branch
if you need an ordered result (order in insert have no sense) you must explict an order by clause in select
select id, productId , branchId
from productopening
order by productId , branchId
or if you need that the creation of the productopening id is ordered then you can use and ordered select
insert into productopening
select product.id, branch.id
from product
crossjoin branch
order by product.id, branch.id
Related
i have three tables.
1st table is tbl_user
id u_name p_list
1 demo1 1,3,
2 demo2 4,3,1,
3 demo3 2,3,
4 demo4 2,5,
Second table is tbl_product
id product_name
1 example1
2 example2
3 example3
4 example4
5 example5
third table is tbl_order_list that show the list of order to the users. tbl look like
id pid uid
1 1 1,2,
2 4 2,
3 3 1,2,3,
4 5 4,
now i want
if i'll update tbl user means if i'll remove pid of column p_list of tbl_user than tbl_user will be updated and also tbl_order_list is update
"Example" if i'll remove 3 in p_list of id 2 from tbl_user than table is look like
id u_name p_list
1 demo1 1,3,
2 demo2 4,1, //(pid =3 is remove of demo2)
3 demo3 2,3,
4 demo4 2,5,
And tbl_order_list is look like
id pid uid
1 1 1,2,
2 4 2,
3 3 1,3, //(uid =2 is remove)
4 5 4,
what is the solution of this problem. i want also if i'll add again pid = 3 in p_list of id = 2 of table user than table look like previous tables.
QUERY (From Comments)
mysql_query("UPDATE tbl_user SET u_name='$userName',slug='$userSlug',password='$userPassword',email='$userEmail',role='$userRole',p_list='$userList',status='$userStatus',modified_date='$userDate' WHERE id='$uId'");
Help Me
So far, I'm not seeing any real correlation to php, but whatever.
I would highly suggest using a normal form (see database normalization) and deduplicate your data. tbl_order_list is a very bad table, as is tbl_user. tbl_order_list shouldn't have an id field, the uid field should contain exactly one uid and the primary key should be (pid,uid) (so both fields). By that point it should be called tbl_order. If you want a list of all pids for a uid or all uids for a pid - at some point - you can query like this:
SELECT GROUP_CONCAT(uid)
FROM tbl_order
WHERE pid=[your pid of interest]
GROUP BY pid
and vice-versa (replace uid with pid and pid with uid). If you want to query it at the same time with the rest of your userdata:
SELECT u.*, GROUP_CONCAT(o.pid)
FROM tbl_user u
LEFT JOIN tbl_order o ON (u.id=o.uid)
[WHERE u.id=[your uid of interest]]
GROUP BY u.id
(and similarly with the tbl_product).
When using mysql >= 5.7 you can create a view:
CREATE VIEW tbl_order_list AS SELECT pid, GROUP_CONCAT(uid) FROM tbl_order GROUP BY pid.
Adding or Removing an order then would result in deleting/inserting a row in tbl_order.
Summary: Make a table tbl_order (uid,pid) that contains the links between uids and pids (one link per row). Join this table in, when you need the the links. Please don't save "references" as a comma-separated list in a string field unless there is a very very very good reason.
If you infact have a very very good reason, consider adding comma (,) to the beginning and end of your lists, meaning. So:
id pid uid
1 1 ,1,2,
2 4 ,2,
3 3 ,1,2,3,
4 5 ,4,
This way you can safely search, add, remove entries:
SELECT * FROM tbl_order_list WHERE uid LIKE '%,[uid of interest],%'
UPDATE tbl_order_list SET uid=CONCAT(uid,',',[uid to add]) WHERE id=[order to update]
UPDATE tbl_order_list SET uid=REPLACE(",[uid to remove],",",",uid) WHERE id=[order to update]
(and similar for your tbl_user, if you're querying by pid in the tbl_order list, drop the id field.)
If you don't prepend a comma, you might at some point have some problems with consistency.
Anyway, I cannot stress this enough: normalize your database structure unless you have very good reasons not to (and be absolutely sure, they really are good reasons, because usually they aren't)
I have two tables items and item_details.
To show the items in the feeds page I use the below JOIN QUERY.
SELECT i.item_id,i.time,id.text,id.photo FROM items i LEFT JOIN item_details id ON i.item_id=id.item_id
but, it returns multiple rows of same item id. The only work around I find is first select the items table only, then select the item_detail table while looping in PHP. I feel that's not a best practice and may effect the performance.
Kindly suggest a better method to select the items.
Problem:
You are joining with item_details which has 3 records with same item_id. So the result will have more records for that item_id.
Solution:
I think this is what you after:
SELECT i.item_id,i.time,MAX(id.text) as text,MAX(id.photo) as photo
FROM items i LEFT JOIN
item_details id ON i.item_id=id.item_id
GROUP BY i.item_id,i.time
Sample result:
item_id time text photo
---------------------------------------------------------------------
2 87213 (null) (null)
4 2029 another text row for item id 4 a photo for item id 4
Sample result in SQL Fiddle
NB: This method will not work as expected if you have multiple records with same item_id having different values for text and photo columns.
I have two table suppose products and auto_assign_prod_list. I want to populate a dropdown list with the id of products table that are not present in auto_assign_prod_list table.
Suppose,
product table contain
Id
------
1
2
3
4
5
auto_assign_prod_list table contain
Id
-----
1
5
So, my result set will be
2
3
4
How is it possible using MySQL and PHP ?
Try this:
SELECT Id FROM product
WHERE Id NOT IN (SELECT Id FROM auto_assign_prod_list)
It will select the ids from product table which are not in auto_assign_prod_list table.
Result:
Id
------
2
3
4
See result in SQL Fiddle.
use a left join
select p.id
from products p
left join auto_assign_prod_list a on a.id = p.id
where a.id is null
SQLFiddle demo
See this great explanation of joins
I have a table setup similarly as below.
genre_id series_id
1 4
1 2
2 5
4 1
2 4
3 3
What I want to do is to be able to find all series based on the mix of genres selected.
For example finding all series that have a genre id of 1 and 2. Hypothetically the result I want is the series ID of 4.
If I use
SELECT series_id FROM table WHERE genre_id = 1 AND genre_id = 2
it returns nothing.
If I use
SELECT series_id FROM table WHERE genre_id in (1, 2)
it returns everything in 1 and 2. But I just want the rows where the genre_id's intersect.
Is there any way to do this?
This should do the trick:
SELECT series_id FROM table
WHERE genre_id IN (1, 2)
GROUP BY series_id
HAVING COUNT(*) = 2
Note this is assuming that the pair (genre_id, series_id) is unique. If it is not you will have to change the HAVING clause to
HAVING COUNT(DISTINCT genre_id) = 2
Also note that the number 2 in the HAVING clause must match the amount of items in the IN clause.
You can think of the IN() predicate as basically a series of OR terms; it's equivalent to
SELECT series_id
FROM MyTable
WHERE genre_id = 1 OR genre_id = 2
What you want is to turn the OR into AND, but that doesn't make any sense because a WHERE expression applies to one row at a time. There's no way genre_id can be both 1 and 2 on the same row.
So you need to compare genre_id from two different rows in one expression. You can do this by joining the two rows:
SELECT t1.series_id
FROM MyTable AS t1
INNER JOIN MyTable AS t2 USING (series_id)
WHERE t1.genre_id = 1 AND t2.genre_id = 2
There's also a solution using GROUP BY as shown in another answer, but the self-join can be orders of magnitude more efficient if you define the right indexes.
I describe more details for this solution in my presentation SQL Query Patterns, Optimized.
I have 2 tables,
Table1:
id, int1, int2, int3
john,1,2,4
tim,2,3,4
pete,1,3,4
Table2:
integer,blob
1,wins
2,backtickle
3,text
4,whatever
The query I want to use is given the id I want to get the blob data from table2 associated with each of the integer columns in table1.
What is the specific query I can use here?
Sample result I am looking for would be something like:
search John returns "wins","backtickle","whatever"
search pete returns "wins","text","whatever"
I think it has something to do with foreign keys, but not sure...beginner level please! With 1 table it would be SELECT * FROM table1 WHERE id="........" but not sure with the setup i have now given above.
The structure of your database does not look optimal. You're limiting yourself to 3 items per person, and you're using columns instead of rows in order to list them. What you actually have in your data is a many-to-many relationship between Table1 and Table2. What I'd recommend is using three tables:
Persons:
name, personid
john,1
tim,2
pete,3
PersonBlobs:
personid, blobid
1,1
1,2
1,4
2,2
2,3
2,4
3,1
3,3
3,4
Blobs:
blobid,blob
1,wins
2,backtickle
3,text
4,whatever
PersonBlobs would give you the many-to-many link between Persons and Blobs.
And then the query becomes:
select Blobs.blob
from Blobs inner join PersonBlobs on Blobs.blobid = PersonBlobs.blobid
inner join Persons on PersonBlobs.personid = Persons.personid
where Persons.name = 'John'