Column count does not match value count at row 1 - php

While deleting, getting error like Column count does not match value count at row 1
Mysql Query:-
DELETE FROM customers WHERE customers.id IN (SELECT cust_id FROM customers_info)

Try this:
DELETE FROM customers WHERE customers.id IN (SELECT GROUP_CONCAT(cust_id) FROM customers_info)

Related

Select rows from 2 tables in 1 query

Need to select name,domain from table 1, than I need a sum of values from a column from table 2 with condition table1.id = table2.
Table 2 does not have the same number of columns.
I have tried joining 2 queries with UNION and UNION ALL, but i keep getting the same problem of different number of columns.
$rows = $test->query('select domain,name from customers
UNION
select SUM(customer_id) from main.rentals,main.customers where customer_id = customers.id');
Expected would be "User's Name" - "domain" - "number of rentals(integer)"
Warning: SQLite3::query(): Unable to prepare statement: 1, SELECTs to the left and right of UNION do not have the same number of result columns
If you use SUM, it will sum all ids, for example if you have a customer ID of 10 and it shows 5 times the result will be 50. In the other hand if you use count it will count the rows that that id was shown. That's why we are doing a group by. You may tweak it to fill your specific needs, but this is one way to achieve what you want.
$query = "
SELECT
domain,
name,
count(customer_id) as Total
FROM
customers
left join main.rentals on customers.id = customer_id
GROUP BY
customer_id
";
$test->query($query );

How to display duplicated results only in MYSQLI?

I want to display only the duplicated results in a PHP page, not their count and there is a condition where certain field can't be empty. What is the mysqli query for displaying each duplicated result and not grouping them so it displays all the duplicates in 1 row rather than displaying each duplicate in a single row?
This is for a reporting panel on full PHP website. I have tried some queries like SELECT col1,col2 FROM table Where col3!='' GROUP BY col2 HAVING COUNT(col1) > 1; But this will display the duplicated results in 1 row, if I have 3 duplicates with same col2 the query returns only one due to the GROUP BY col2 clause, I tried GROUP BY col2,col1 but now HAVING COUNT(col1) > 1 can never be true since it is impossible to have same value of col1.
I expect the output of the query to display each duplicated result in a row and not group them in 1 row only. In other words to display the same 3 duplicated results having same col2 but different other columns values and not display only their 1 result.
All results:
Query I tried:
Try this
SELECT * FROM usersl Where hash!='' AND user_fullName IN (SELECT user_fullName FROM usersl HAVING COUNT(user_fullName) > 1);
This will give you result as you want
I think you should add one more column in group by clause and that column should be Primary ID column then you will got your desired result.
SELECT col1,col2 FROM table Where col3!='' GROUP BY col2,pk_id;
Try this. I think this helps you.
SELECT u1.* FROM usersl AS u1
INNER JOIN (SELECT user_fullName FROM usersl WHERE hash != '' GROUP BY (user_fullName) HAVING COUNT(user_fullName) > 1) AS u2 ON u1.user_fullName = u2.user_fullName
WHERE u1.hash!=''
First find all ID's of these fields:
$ids = $db->queryColumn(
"SELECT GROUP_CONCAT(id SEPARATOR ',') FROM table WHERE col3 != '' GROUP BY col2 HAVING COUNT(col1) > 1"
);
Then make array of ID's:
$idsToFind = [];
foreach ($ids as $idString) {
array_push($idsToFind, array_walk('trim', explode(',', $idString)));
}
Now do another query where you find elements by ID:
$db->queryAll(
'SELECT col1, col2 FROM table WHERE id IN :idArray',
$this->escapeArray($idsToFind)
);

What type of index should I to add on a multiple request_id in mysql

Data are taking too much time to load when I am searching with specified date. In my project has two table, In one table has unique entries and I added unique index to "request_id" and primary index to auto incremented "id" . another table have multiple records with request_id and in that I added only a primary index to auto incremented "id". Now I am to search these all record through join in both table to check the count for a every "request_id".
I am using below query:-
SELECT
m.id,m.request_id as id,count(m.request_id) as count,m.reqtype,m.request_time,w.status as status,w.updated_time as updated_time,w.reg_date as reg_date
FROM
multi_requests m JOIN unique w ON m.request_id = w.request_id
WHERE
m.request_time
Between
'2015-07-05'
AND
'2015-07-06'
GROUP BY
m.request_id
ORDER BY
m.id asc
LIMIT
0,10" ;
I also try to add index to "request_id" in multi_requests table. But when I am adding Index to "request_id" and searching with above query its not showing any type of records on UI.
In multi_requests table has total records = 6033030.
So please suggest me..
This is your query:
SELECT m.id, m.request_id as id, count(m.mac_address) as count, m.reqtype,
m.request_time, w.status as status, w.updated_time as updated_time,
w.reg_date as reg_date
FROM multi_requests m JOIN
unique w
ON m.mac_address = w.mac_address
WHERE m.request_time Between '2015-07-05' AND '2015-07-06'
GROUP BY m.request_id
ORDER BY m.id asc
LIMIT 0, 10 ;
It is a bit strange, because you have a ton of columns in the select, but only one in the group by. Let me assume that you know what you are doing.
For this query, the best indexes are on multi_requests(request_time, mac_address, request_id) and unique(mac_address).

SQL DELETE highest value

I have a table with a list of rooms with each assigned a room number. I would like to delete the highest room number:
DELETE FROM rooms WHERE roomNb = (SELECT max(roomNb) FROM rooms LIMIT 1)
I would expect this to work but when I try to execute this query in PHP I get this mysqli_error:
string(64) "You can't specify target table 'rooms' for update in FROM clause"
I don't see a reason to use a subquery/join... why not just shift the row off the top of a table after an ORDER BY clause?
DELETE FROM rooms ORDER BY roomNb DESC LIMIT 1
try using join,
DELETE a
FROM rooms a
INNER JOIN (SELECT max(roomNb) maxroom FROM rooms) b
ON a.roomNb = b.maxroom

Selecting rows from a table by One a field from other table

What i want, to display rows from a table which is selected by a field from other table single value, lets say to display images from a table by last category id.
I have this type of query, but this return me all matching keys rows, if i inset LIMIT 1 then it return one row...
SELECT i.prof_image FROM profile_images i
JOIN images_cat cat ON (cat.cat_id = i.cat_id)
GROUP BY i.prof_image;
//OR LIMIT 1;
Any idea to fix this problem. (i.e. displaying the latest category images)?
This will work for your specific example.. If you need to be more selective, then please post some more details..
SELECT i.prof_image
FROM profile_images i
WHERE cat_id = (select max(cat_id) from images_cat)
SELECT * FROM table_1
LEFT JOIN table_2 ON table_1.id = table_2.id
This query will grab all things in table_2 that have the same id value.
Note that it is a LEFT JOIN - which means that if there are no matching values in table_2, it will still return the values from table_1.
What is your intent with using last()?
Hope this helps.

Categories