Dear friends, I really have a serious problem, I have several tables that store certain information of users such as their facebook, twitter, instagram, image, email, etc ...
I will show the SQL and explain the problem:
SELECT
IFNULL(empresa_twitter.twitter,users.twitter) AS user_twitter,
users.id AS user_id,
users.status AS user_status,
users.grants AS user_grants,
Lower(users.admin) AS user_admin,
users.webmail AS user_webmail,
users.PLAN AS user_plan,
users.username AS user_username,
users.facebook AS user_facebook,
users.instagram AS user_instagram,
users.twitter AS user_twitter,
Trim(Lower(users.nombre))AS user_nombre,
Trim(users.about) AS user_about,
Lower(users.email) AS user_email,
IFNULL(Lower(empresa_imagen.imagen),users.imagen) AS user_imagen,
IFNULL(Lower(empresa_facebook.facebook),users.facebook) AS user_facebook,
IFNULL(Lower(empresa_instagram.instagram),users.instagram) AS user_instagram,
Lower(paises.pais) AS pais_nombre,
Lower(paises.iso2) AS pais_code,
Lower(paises.moneda) AS pais_moneda,
Lower(paises.simbolo_moneda) AS pais_simbolo,
Lower(paises.bandera) AS pais_bandera,
IFNULL(SUM(ingresos.monto), 0) AS ingresos,
IFNULL(SUM(egresos.monto), 0) AS egresos
FROM users, paises
LEFT JOIN ingresos ON ingresos.user = 1907
LEFT JOIN egresos ON egresos.user = 1907
LEFT JOIN empresa_imagen ON empresa_imagen.user = 1907
LEFT JOIN empresa_facebook ON empresa_facebook.user = 1907
LEFT JOIN empresa_instagram ON empresa_instagram.user = 1907
LEFT JOIN empresa_twitter ON empresa_twitter.user = 1907
WHERE users.id = 1907
AND paises.iso2 = users.pais
The problem exists when in the first SELECT I look for the twitter in the table empresa_twitter, since it shows me the twitter stored in the users table in case company_twitter is NULL or empty, but:
It does not show me the most recent record (the last record), I try to make a MAX (IFNULL ()) but it causes me an error, I just want to get the last record of your twitter, facebook, instagram, image, etc ... But at the same time add it to the results ...
1, user, email, facebook.com, twitter.com, instagram.com, etc ...
Company_facebook table
_____________________________________________________________ | ID | user | user_facebook | fecha | -------------------------------------------------------------- | 56 | 1907 | axer.xiche | 0000-00-00 00:00:00 | -------------------------------------------------------------- | 57 | 1907 | arc.guevara | 0000-00-00 00:00:00 | --------------------------------------------------------------
Table users
_________________________________________________________________ | ID | username | user_facebook | fecha | ------------------------------------------------------------------ | 1907 | arcaelas | usuario_default | 0000-00-00 00:00:00 | ------------------------------------------------------------------
Now what I need is for him to select the fields in the users table where id = 1907 and the last record in the twitter_table table, but in case there is no record in the table empresa_twitter show me the result of the users table, I did it with a
IFNULL(empresa_twitter.twitter, users.twitter)
And there we go well, but everything gets damaged when I want to show me the last record since I tried it with
MAX(IFNULL(empresa_twitter.twitter, users.twitter)) LAST(IFNULL(empresa_twitter.twitter, users.twitter))
And they return error, what should I do or how should I execute the query?
What I try is not to perform separate queries to avoid more weight on the server, since MySql knows how it works, unlike Postgrel, Whoever has a good solution would appreciate it in the soul and the company too.