MySQL, move data from rows to columns with Images pictures - mysql

table1
id id_customer name_photo1 photo_1 name_photo2 photo_2
1. 1 pic.png *sdhsadj jsjs.png *ssksksks
2. 2 pic2.png *sadjsad sdds.png *ssddsdsdw
To table2
id. id_customer namefoto picture
1. 1 pic.png *sdhsadj
2 1 jsjs.png *ssksksks
3. 2 pic2.png *sadjsad
4. 2 sdds.png *ssddsdsdw
As I can move the data with a query from the table to the table2, the column photo_1 and photo_2 is longblob.
Thanks

You need to "unpivot" to convert columns into records.
You can do it in one query with INSERT INTO .. SELECT in combination with UNION ALL
INSERT INTO
table2
(
table2.id_customer
, table2.namefoto
, table2.picture
)
SELECT
records.id_customer
, records.namefoto
, records.picture
FROM (
SELECT
table1.id_customer AS id_customer
, table1.name_photo1 AS namefoto
, table1.photo_1 AS picture
FROM
table1
UNION ALL
SELECT
table1.id_customer AS id_customer
, table1.name_photo2 AS namefoto
, table1.photo_2 AS picture
FROM
table1
) AS records
ORDER BY
records.customer_id ASC

You can do it with INSERT INTO ... SELECT statement:
INSERT INTO
table2(id_customer, namefoto, picture)
SELECT
id_customer,
name_photo1,
photo_1
FROM
table1;
INSERT INTO
table2(id_customer, namefoto, picture)
SELECT
id_customer,
name_photo2,
photo_2
FROM
table1;

Related

If there is a way to achieve this kind of result below using group_concat

Data
Brcode name
1. A
2. A
3. A
Expected result
Brcode Name Common
1 A 1,2,3
2 A 1,2,3
3 A 1,2,3
Yes you can do a join.
CREATE TABLE test_tbl (
Brcode int(9),
name varchar(3) );
INSERT INTO test_tbl VALUES (1,'A'),
(2,'A'),
(3,'A');
SELECT t1.Brcode,
t1.name,
t2.common
FROM test_tbl t1
inner join
(
select name ,group_concat(Brcode SEPARATOR ',') as Common from test_tbl group by name
) as t2 on t1.name=t2.name;
Demo: https://www.db-fiddle.com/f/7yUJcuMJPncBBnrExKbzYz/99
Result:

MySql: How to select rows where all values are the same?

I have a table like this:
name |id | state
name1 12 4
name1 12 4
name2 33 3
name2 33 4
...
I want to select every name and id from table where state is only 4, that means name1 is correct, because it only has two records with state 4 and nothing more. Meanwhile name2 is wrong, because it has record with state 4 and record with state 3.
You can use aggregation as shown below:
SELECT name, id
FROM your_table
GROUP BY name, id
HAVING SUM(state<>4)=0;
See a Demo on SQL Fiddle.
select name, id from mytable where id not in
(select distinct id from mytable where state <> 4)
you might need 2 sub queries .
select with group by name were state 4
select with group by name
compare the count if the count is same then select it
example : select name , count (name) from table where state = 4 as T1
select name , count (name) from table as T2
select T1.name from T1 and T2 where T2.count = T1.count
You can use not exists like this:
select distinct name, id
from table1 a
where not exists (select *
from table1 b
where a.id=b.id and state<>4)
In a more general case you can use count distinct (with not exists or with a join):
select distinct name, id
from table1 a
where not exists (
select *
from table1 b
where a.id=b.id
group by id
HAVING count(distinct state)>1)

How to use MySQL UNION and SUM in views

I have 2 tables
table one :
id amount
1 2
1 1
2 1
table two :
id amount
1 2
I want make view with the output like this :
view :
id amount
1 5
2 1
I tried with my simple code like this
CREATE VIEW v_test AS
SELECT id, sum(amount)
FROM table1
UNION ALL
SELECT id, sum(amount)
FROM table2
GROUP BY id
but when i run the SQL above the output like this :
id amount
1 2
1 3
2 1
any code for the output that i want?
thanks
Looking to your data sample and do the fact that some mysql version don't allow subselect in view you can create a utility view
create view v_test_union
as
select id, amount
from table1
union all
select id, amount
from table2
then create your view
create view
as
select id, sum(amount)
from v_test_union
If you use mysql 5.7 or above, you can use subquery in the create view statement:
create view
as
select id, sum(amount)
from
(select id, amount
from table1
union all
select id, amount
from table2) t
group by id
you can also use this by using join like below
CREATE VIEW v_test AS
SELECT table1.id as id,(sum(table1.amount)+sum(table2.amount)) as amount FROM table1
join table2 on table1.id=table2.id
GROUP BY table1.id
SQL DEMO
CREATE VIEW v_test AS
SELECT id, SUM(amount)
FROM (
SELECT id, amount
FROM table1
UNION ALL
SELECT id, amount
FROM table2
) t
GROUP BY id
;
OUTPUT

Insert into table with multiple rows in subquery

I want to have results in a table where the data comes from 3 different tables.
For that I have tried to execute this query:
INSERT INTO sometable (id,date)
VALUES
(
(SELECT id FROM table1
UNION
SELECT id FROM table2
UNION
SELECT id FROM table3)
,
(SELECT date FROM table1
UNION
SELECT date FROM table2
UNION
SELECT date FROM table3)
)
The result of this query is an error stating cannot insert multiple rows. Please help me to write this query correctly.
The INSERT ... SELECT syntax is different to the INSERT ... VALUES syntax. Also, you want to select both columns from each table at the same time:
INSERT INTO sometable (id, date)
SELECT id, date FROM table1 UNION
SELECT id, date FROM table2 UNION
SELECT id, date FROM table3

whats wrong with this mysql insert query?

insert into tblcustomermachine
(
select * from
((select vch_CustomerID from tblcustomer where tblcustomer.vch_CustomerID='Cust00001' )
union all
(select Rate from tblmachine)) as t );
that table contains 18 cols and this resultset also contains 18 rows yet it shows " Column count doesn't match value count at row 1" . why?
It looks like your table tblcustomermachine has more then the 1 column.
Like Simone answered, update your insert to INSERT INTO tblcustomermachine(col_1) SELECT ...
You may skip the column names during INSERT, however the SELECT needs to return the same amount of columns that the table holds.
AFAIK, you have to declare field name:
insert into tblcustomermachine (col_1, col_2, col_3, ... col_18) (
select t.field1, t.field2, t.field3, ... t.field18 from (
(select vch_CustomerID from tblcustomer where tblcustomer.vch_CustomerID='Cust00001')
union all (select Rate from tblmachine))
as t
);